问题描述
用结构体数组的方法改写例9-1
【例9-1】按表9-1的形式从键盘依次输入每个学生的学号、姓名、出生年月、3门课的成绩,计算并打印出每个学生的平均成绩。
输入
第一行,整数n,表示一共有n个学生。
从第二行开始共n行,每行包含学号,姓名,出生年月,数学,英语,C语言,空格隔开,姓名不含空格,出生年月分开输入
输出
共n行,每行包含学号,姓名,出生年/月,数学,英语,C语言,平均成绩。
输出浮点数使用%.0f,出生年月用/分开。
样例输入
2
901 hulei 1990 8 67 78 89
902 fangang 1991 7 85 69 76
样例输出
901 hulei 1990/8 67 78 89 78
902 fangang 1991/7 85 69 76 77
AC代码
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
typedef struct Stu{
int num;
char name[20];
int year;
int month;
int math;
int english;
int C_language;
float ave;
};
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++)
{
Stu stu;
cin >> stu.num >> stu.name >> stu.year >> stu.month >> stu.math >> stu.english >> stu.C_language;
stu.ave = ceil((stu.math + stu.english + stu.C_language) / 3.0);
cout << stu.num << " "<< stu.name << " " << stu.year << "/" << stu.month << " " << stu.math << " " << stu.english << " " << stu.C_language << " ";
printf("%.0f\n",stu.ave);
}
}