初次使用结构体解决学生成绩排列问题
2016-01-06 17:10
253 查看
Sorting students on grades
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. The last line contains the 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
Sample Output
代码如下:
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. The last line contains the 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<bits/stdc++.h> using namespace std; struct stu//首先创建一个名为stu的结构体,里面有学生的id跟学生的成绩。 { int id; int grade; }; int cmp(stu a,stu b) { return a.grade<b.grade;//定义一个排序规则,通过学生的成绩进行排序 } int main() { int m; cin>>m; for(int h=0;h<m;h++) { int n; cin>>n; stu studen[101];//创建studen结构体 char score[n+1][10];//这是记录成绩的数组 for(int i=0;i<n+1;i++) { for(int j=0;j<10;j++) { cin>>score[i][j]; } } for(int i=0;i<n;i++) { int count=0; for(int j=0;j<10;j++) { if(score[i][j]==score [j]) count++; } studen[i].id=i;//通过studen.id或studen.grade来调用结构体内的内容。 studen[i].grade=count; } sort(studen,studen+n,cmp);//用学生的成绩进行排序,排序完后id与成绩仍保持对应关系。只不过对应的结 cout<<"test case "<<h+1<<":"<<endl; //构体不同。 for(int i=0;i<n;i++) { cout<<"Student "<<studen[i].id<<": "<<studen[i].grade<<endl; } } return 0; }
相关文章推荐
- 在Python里画曲线
- 《人人都是产品经理》 学习笔记1
- 安装centreon+nagios时,rrd出图出现乱码
- 图片开放接口_免费的美女图片API接口
- linux下tty,控制台,虚拟终端,串口,console(控制台终端)详解
- android WebView
- plist文件的读写
- halcon自动全局阈值与动态阈值分割方法
- Docker
- C# 利用QRCode生成二维码图片
- HanLP自然语言处理包开源(包含源码)
- Unable to execute dex: Multiple dex files define异常的解决办法
- 64位程序core分析
- 【bzoj1853】【Scoi2010】【幸运数字】【搜索】
- javascript类型系统之基本数据类型与包装类型
- Dango Web 开发指南 学习笔记 2
- Ecplise代码单步调试
- 系统相册和照相机
- 利用HTML5开发Android
- velocity.js学习(一)