Description
Rewrite Listing 6.12, GradeExam.cpp, to display the students in increasing order of the number of correct answers.
Suppose the answers for all students are stored in a two-dimensional array.
Each row records an student’s ten answers with ten columns.
For example, the following array stores the answers for 3 students.
0 1 2 3 4 5 6 7 8 9
Student 0 A B A C C D E E A D
Student 1 D B D C C D A E A D
Student 2 E D D A C B E E A D
The key is stored in a one-dimensional array, as follows:
0 1 2 3 4 5 6 7 8 9
Key D B D C C D A E A D
Input
The first line is a positive integer t for the number of test cases.
Each test case contains m+2 lines.
The line 1 contains an integer m (0<m<=100) for number of the stuents.
Then followed m lines, each line contains 10 integers seperated by blanks, for one student’s answers.
And the following line contains 10 keys of correct answers.
Output
For each test case,output each student’s number and the number of correct answers in increasing order of the number of correct answers. Use the format like the sample output.
Sample Input
2
3
A B A C C D E E A D
D B D C C D A E A D
E D D A C B E E A D
D B D C C D A E A D
2
B B E C C D E E A D
A B D C C D E E A D
A B D C C D E E B D
Sample Output
test case 1:
Student 2: 5
Student 0: 7
Student 1: 10
test case 2:
Student 0: 7
Student 1: 9
作业记录。依旧是被我写得冗长繁琐的代码,尽量解释清楚。
#include<stdio.h>
void score(int);
int main(){
int t;
scanf("%d\n",&t);
int record[t];
for(int i=0;i<t;i++){
printf("test case %d:\n",i+1);
scanf("%d\n",&record[i]);
//这里对每一组数据调用函数进行操作
score(record[i]+1);
}
return 0;
}
//为了降低复杂度,写了这个函数补充score函数
//建议先阅读score函数代码
void array(int count[],int n){
int f[11]={0,1,2,3,4,5,6,7,8,9,10};
//上面那个数组包含了所有可能的成绩
int re[11][n];
//这个数组用来记录最终要输出的成绩
//11表示11种成绩,n表示拿这个成绩的人数
//用二维数组是为了防止同分的情况出现
for(int k=0;k<11;k++){
for(int c=0;c<n;c++){
re[k][c]=-1;
}
}
//通过循环先将所有学生的成绩初始化为-1
//因为学校平台的编译器好像不支持直接全部赋值为-1
int i=0;
for(i=0;i<n;i++){
for(int c=0;c<11;c++){
if(f[c]==count[i]){
re[f[c]][i]=i;
break;
}
}
//在保证count数组里面学生号和成绩一一对应的情况下
//用re数组记录学生编号,索引f[c]表示成绩,索引[i]就是拿来占个位置
}
for(int k=0;k<11;k++){
for(int c=0;c<n;c++){
if(re[k][c]>-1)printf("Student %d: %d\n",re[k][c],k);
}
}
}
void score(int n){
char t[n][10];
int count[n-1];
for(int i=0;i<n;i++){
for(int k=0;k<10;k++){
scanf("%c ",&t[i][k]);
}
}
//上面的循环获取学生答题情况
for(int i=0;i<n-1;i++){
count[i]=0;
for(int k=0;k<10;k++){
if(t[i][k]==t[n-1][k])count[i]++;
}
}
//上面的循环通过将学生答案与正确答案对比获得每个学生的分数
//并记录进count数组
array(count,n-1);
//调用array函数完成剩余计算
}
6.20与这题类似
如果想看6.20可以再戳我,谢谢