1258 - C语言10.28
有一个班有4个学生,5门课程。分别完成三个函数,实现以下三个要求:
1、 求第一门课程的平均分;
2、 找出有2门(含2门)以上课程不及格(成绩小于60分)的学生,输出他们的学号和全部课程成绩及平均成绩;
3、 找出平均成绩在90分(含90分)以上或全部课程成绩在85分(含85分)以上的学生。
Input
共有4行,每行有6个用空格隔开的整数,第一个整数表示学生的学号,之后的5个整数表示这个学生所有5门课程的成绩。输入保证所有学号不包含前导0,所有成绩都在0至100之间(包含0和100)。
Output
第一行输出第一门课程的平均分,保留2位小数。
对于每一个有2门(含2门)以上课程不及格的学生,在一行内输出其学号、5门课程的成绩以及平均成绩,用空格隔开。请注意,平均成绩保留2位小数,且之后不需要输出空格。如果有多个满足条件的学生,按照输入顺序依次输出。
最后一行输出所有满足第3条要求的学生的学号,每个学号后输出一个空格。如果有多个满足条件的学生,按照输入顺序依次输出。
请注意行尾输出换行。
Examples
Input
20120003 85 86 85 89 89 20120001 75 59 55 90 88 20120015 65 55 60 85 80 20120009 80 95 95 90 90
Output
76.25 20120001 75 59 55 90 88 73.40 20120003 20120009
Solution C
#include<stdio.h> #include<string.h> int main() { int xue[4],a[4][5],i,k; double s=0,w=0,x[4]={0},y[4]={0}; for(i=0;i<4;i++) { scanf("%d",&xue[i]); for(k=0;k<5;k++) { scanf("%d",&a[i][k]); } } for(i=0;i<4;i++) { s += a[i][0]; } s=s/4.; printf("%.2lf\n",s); for(i=0;i<4;i++) {s=0; for(k=0;k<5;k++) { if(a[i][k]<60) s++; } for(k=0;k<5;k++) { if(a[i][k]>=85) x[i]++; else break; } if(s>=2) { printf("%d ",xue[i]); for(k=0;k<5;k++) { printf("%d ",a[i][k]); w+=a[i][k]; } w=w/5; printf("%.2lf\n",w); } for(k=0;k<5;k++) { y[i]+=a[i][k]; } y[i]=y[i]/5; } for(i=0;i<4;i++) { if(x[i]==5) printf("%d ",xue[i]); else if(y[i]>=90) printf("%d ",xue[i]); } printf("\n"); return 0; }
Solution C++
#include <stdio.h> int main() { void request1(int id[], int score[][5], int n); void request2(int id[], int score[][5], int n); void request3(int id[], int score[][5], int n); int id[4], score[4][5]; int i, j; for (i = 0;i < 4;i++) { scanf("%d", &id[i]); for (j = 0;j < 5;j++) scanf("%d", &score[i][j]); } request1(id, score, 4); request2(id, score, 4); request3(id, score, 4); return 0; } /* 第一个要求 */ void request1(int id[], int score[][5], int n) { int i; float total_score, aveg_score; total_score = 0; for (i = 0;i < n;i++) total_score += score[i][0]; aveg_score = total_score / n; printf("%.2f\n", aveg_score); } /* 第二个要求 */ void request2(int id[], int score[][5], int n) { int i, j, less_than_60; float total_score, aveg_score; for (i = 0;i < n;i++) { /* 统计该学生有多少门不及格 */ less_than_60 = 0; for (j = 0;j < 5;j++) if (score[i][j] < 60) less_than_60++; if (less_than_60 >= 2) { printf("%d ", id[i]); total_score = 0; for (j = 0;j < 5;j++) { printf("%d ", score[i][j]); total_score += score[i][j]; } aveg_score = total_score / 5; printf("%.2f\n", aveg_score); } } } /* 第三个要求 */ void request3(int id[], int score[][5], int n) { int i, j, not_less_than_85; float total_score, aveg_score, eps = 1e-5; for (i = 0;i < n;i++) { total_score = 0; not_less_than_85 = 0; /* 统计该学生有多少门在85分(含)以上,并计算平均分 */ for (j = 0;j < 5;j++) { if (score[i][j] >= 85) not_less_than_85++; total_score += score[i][j]; } aveg_score = total_score / 5; /* 这里的eps的作用是保证实数大小比较的准确性 */ if (not_less_than_85 == 5 || aveg_score >= 90 - eps) printf("%d ", id[i]); } printf("\n"); }