有n个学生m个课程
求每个学生的平均分
求每个课程的平均分
求每个课程分数大于该课程平均分的人数
#include <stdio.h> #include <math.h> int main() { int m, n, i, j, count,sum; while (scanf_s("%d%d", &n, &m)) { count = sum = 0; double defen, pdefen[50][5] = { { 0 } }; double p[50] = { 0 }; double q[5] = { 0 }; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { scanf_s("%lf",&defen); pdefen[i][j] = defen; } } for (i = 0; i < n; i++) { for (j = 0;j < m;j++) { p[i] += pdefen[i][j]; } printf("%.2lf%c", p[i] / m, (i < n - 1 ? ‘ ‘ : ‘\n‘)); } for (j = 0; j < m; j++) { for (i = 0; i < n; i++) { q[j] += pdefen[i][j]; } printf("%.2lf%c", q[j] / n, (j < m - 1? ‘ ‘ : ‘\n‘)); } for (i = 0; i < n; i++) { count = 0; for (j = 0; j < m && pdefen[i][j] >= q[j]/n; j++) { count++; if (count == m) sum++; } } printf("%d\n", sum); } return 0; }
参考答案
#include <stdio.h> #include <string.h> int main(void) { int n, m; int i, j; int t, d; int s[50]; int c[5]; int sc[50][5]; while (scanf("%d%d", &n, &m) != EOF) { memset(s, 0, sizeof(s)); memset(c, 0, sizeof(c)); memset(sc, 0, sizeof(sc)); for (i = 0 ; i < n ; i++) { for (j = 0 ; j < m ; j++) { scanf("%d", &sc[i][j]); c[j] += sc[i][j]; s[i] += sc[i][j]; } } for (i = 0 ; i < n ; i++) printf("%.2lf%c", s[i] * 1.0 / m, i < n - 1 ? ‘ ‘ : ‘ \n‘); for (i = 0 ; i < m ; i++) printf("%.2lf%c", c[i] * 1.0 / n, i < m - 1 ? ‘ ‘ : ‘ \n‘); for (t = i = 0 ; i < n ; i++) { for (d = 1, j = 0 ; j < m ; j++) { if (sc[i][j] < 1.0 * c[j] / n) { d = 0; break; } } if (d) t++; } printf("%d\n\n", t); } return 0; }
时间: 2024-09-29 10:20:13