80 平均分
作者: 江宝钏时间限制: 1S章节: 结构体
问题描述 :
从键盘依次输入每个学生的学号、姓名、出生年月、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
笔记
特别注意,在创建结构体变量时,要在结构体名称前加上“struct”:
struct student{
int no;
char name[10];
int birth_y;
int birth_m;
int math_score;
int e_score;
int c_score;
};
struct student stu;
否则会出现编译错误。
代码
#include<stdio.h>
struct student{
int no;
char name[10];
int birth_y;
int birth_m;
int math_score;
int e_score;
int c_score;
};
int main(){
int n;
scanf("%d",&n);
int i;
struct student stu;
int avg_score = 0;
for(i=0;i<n;i++){
scanf("%d %s %d %d %d %d %d",
&stu.no,stu.name,&stu.birth_y,&stu.birth_m,&stu.math_score,&stu.e_score,&stu.c_score);
printf("%d %s %d/%d %d %d %d %.0f\n",
stu.no,stu.name,stu.birth_y,stu.birth_m,stu.math_score,stu.e_score,stu.c_score,
1.0*(stu.math_score+stu.e_score+stu.c_score)/3);
}
return 0;
}