求平均成绩
1 #include <cstdio> 2 3 struct Stu 4 { 5 double score[5]; 6 double sum; 7 double avg; 8 }; 9 10 int main() 11 { 12 int n, m; 13 while(scanf("%d %d", &n, &m) != EOF) 14 { 15 Stu stu[n]; 16 double ke[m] = {0}, ke_avg[m] = {0}; 17 18 for(int i = 0; i < n; ++ i) 19 { 20 stu[i].sum = 0; 21 for(int j = 0; j < m; ++ j) 22 { 23 scanf("%lf", &stu[i].score[j]); 24 stu[i].sum += stu[i].score[j]; 25 } 26 stu[i].avg = stu[i].sum / m; 27 } 28 29 for(int i = 0; i < n; ++ i) 30 { 31 if(i) printf(" %.2f", stu[i].avg); 32 else printf("%.2f", stu[i].avg); 33 } 34 printf("\n"); 35 36 for(int j = 0; j < m; ++ j) 37 { 38 for(int i = 0; i < n; ++ i) 39 { 40 ke[j] += stu[i].score[j]; 41 } 42 ke_avg[j] = ke[j] / n; 43 } 44 for(int i = 0; i < m; ++ i) 45 { 46 if(i) printf(" %.2f", ke_avg[i]); 47 else printf("%.2f", ke_avg[i]); 48 } 49 printf("\n"); 50 51 int count = 0; 52 for(int i = 0; i < n; ++ i) 53 { 54 bool flag = true; 55 for(int j = 0; j < m; ++ j) 56 { 57 if(stu[i].score[j] < ke_avg[j]) 58 { 59 flag = false; 60 break; 61 } 62 } 63 if(flag) count ++; 64 } 65 printf("%d\n\n", count); 66 } 67 68 69 return 0; 70 }
在循环中每个学生的sum初始都要清零(第20行代码)
Wrong Answer(第20行, 结构体中的sum没有初始化)->Accepted