第7回 (6/17) 演習の解答例

Problem 1

#include <stdio.h>

#define MAX_M 100
#define MAX_N 100

int main(void)
{
    float x[MAX_N][MAX_M];
    int i, j, N, M;
    scanf("%d", &N);
    scanf("%d", &M);
    for (i=0; i<N; i++) {
        for (j=0; j<M; j++) {
            scanf("%f", &x[i][j]);
        }
    }
    for (j=0; j<M; j++) {
        float s=0;
        for (i=0; i<N; i++) {
            s += x[i][j];
        }
        printf("%f ", s);
    }
    printf("\n");
    
    return 0;
}

Problem 2

#include <stdio.h>

int main(void)
{   
  struct DATE {
    int year;
    int month;
    int day;
  };

  struct DATE date1, date2;
  int x;
  scanf("%d %d %d", &date1.year, &date1.month, &date1.day);
  scanf("%d %d %d", &date2.year, &date2.month, &date2.day);
  x = (date1.year*10000+date1.month*100+date1.day) - (date2.year*10000+date2.month*100+date2.day);
  if (x < 0) { printf("<\n"); }
  else if ( x > 0) { printf(">\n"); }
  else { printf("=\n"); }
  
  return 0;
}

Problem 3

#include <stdio.h>

#define MAX_N 100

int main()
{   
  struct DATE {
    int year;
    int month;
    int day;
  };

  struct DATE date[MAX_N];
  int i, j, N;

  scanf("%d", &N);
  for (i=0; i<N; i++)
  {
    scanf("%d %d %d", &date[i].year, &date[i].month, &date[i].day);
  }

  for (i=N-1; i>=1; i--)
  {
    for (j=1; j<=i; j++)
    {
      if (date[j-1].year*10000+date[j-1].month*100+date[j-1].day
          > date[j].year*10000+date[j].month*100+date[j].day)
      {
        struct DATE temp = date[j-1];
        date[j-1] = date[j];
        date[j] = temp;
      }
    }
  }

  for (i=0; i<N; i++)
  {
    printf("%d %d %d\n", date[i].year, date[i].month, date[i].day);
  }

  return 0;
}

Updated: