题目描述
从键盘依次输入每个学生的学号、姓名、出生年月、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
本题需注意:
- 输入n后有一个回车可以getchar()吃掉(个人习惯我会写上)
- 输入一行数据之后有一个回车也可以getchar()吃掉
- 使用 cin 输入的数据,同样使用 cout 输出,最后的平均分使用 printf 单独输出(混用的话不一定出错,但这是代码规范)。如果使用 cin 输入了学生姓名 hulei 而使用 printf 将其输出,输出的其实是乱码。
- 回车可以不吃,不写getchar()同样可以AC
AC代码
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
//结构体数组的写法
struct student
{
int id;//学号
string name;//姓名
int year;//年份
int month;//月份
int math_score;//数学成绩
int eng_score;//英语成绩
int c_score;//c语言成绩
}stu[100010];
int main()
{
int n;
cin >> n;
//输入n,吃回车
getchar();
double ave_score;//平均分
for (int i = 0;i < n;i++)
{
//cin输入数据
cin >> stu[i].id >> stu[i].name >> stu[i].year >> stu[i].month;
cin >> stu[i].math_score >> stu[i].eng_score >> stu[i].c_score;
//输入一行数据,吃回车
getchar();
//除数,需要在结果前面强制类型转换(double)
ave_score = (double)(stu[i].math_score + stu[i].eng_score + stu[i].c_score)/3;
//cout输出结果
cout << stu[i].id << " " << stu[i].name << " " << stu[i].year << "/" << stu[i].month << " ";
cout << stu[i].math_score << " " << stu[i].eng_score << " " << stu[i].c_score << " ";
printf("%.0f\n",ave_score);
}
return 0;
}