这题有最开始闪现了两种做法,一种结构体,一种二位数组,最后觉得结构体更形象化,果断选择结构体,表示这题有个易错点,要用double型存储,我也差点没过,
#include <stdio.h> #include <stdlib.h> struct student { double score[6];//每门成绩 double av;//平均成绩 int design;//标记是否大于平均成绩 }stu[55]; int main ( ) { int n,m,i,j,c; while ( scanf ( "%d%d",&n,&m ) == 2 ) { double sum[6] = {0,0,0,0,0,0},ave[6]; c = 0; for ( i = 0 ; i < n; ++i ) { stu[i].av = 0; for ( j = 0; j < m ; ++j ) { scanf ( "%lf",&stu[i].score[j] ); stu[i].design = 1;//将标记初始化 stu[i].av += stu[i].score[j];//求每个学生的总成绩 sum[j] += stu[i].score[j];//求每门成绩的和 } stu[i].av = stu[i].av / m;//每个学生的平均成绩 } for ( j = 0 ; j < m; ++j )//每门课的平均成绩 ave[j] = sum[j] / n; for ( i = 0 ; i < n ; ++i ) { i == 0 ? printf ( "%.2lf" ,stu[i].av ):printf( " %.2lf",stu[i].av );//输出每个学生的平均成绩 for ( j = 0; j < m; ++j ) if ( stu[i].score[j] < ave[j] )//遍历学生以找出大于平均成绩的学生 stu[i].design = 0; if ( stu[i].design == 1 )//统计大于平均成绩的学生 ++c; } printf ( "\n" ); for ( j = 0 ; j < m ; ++j ) j == 0 ? printf ( "%.2lf",ave[j] ) : printf ( " %.2lf",ave[j] );//输出每门成绩的平均分 printf ( "\n" ); printf ( "%d\n",c ); } return 0; }