[Arrays]D. Liang 6.19 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.

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

Problem Source: 程序设计I Chapter6 Arrays

//   Date:2020/4/24
//   Author:xiezhg5
#include <stdio.h>
#include <stdlib.h>
typedef struct NODE {
	int num;
	int count;
} NODE;  //定义结构体变量NODE
//等下调用排序函数qsort
int cmp(const void *p1, const void *p2) {
	NODE *c = (NODE*)p1;
	NODE *d = (NODE*)p2;
	if(c->count != d->count)
		return c->count - d->count;  //按成绩降序排列
	else
		return c->num - d->num;    //按序号降序排列
}
int main(void) {
	int m;
	scanf("%d\n",&m);
	for(int k=0; k<m; k++) {
		int n;
		scanf("%d\n",&n);
		struct NODE student[101];   //创建studen结构体
		char score[n+1][10];       //这是记录成绩的数组
		for(int i=0; i<n+1; i++) {
			for(int j=0; j<10; j++) {
				scanf("%c ",&score[i][j]);
			}
		}
		for(int i=0; i<n; i++) {
			int grade=0;
			for(int j=0; j<10; j++) {
				if(score[i][j]==score[n][j]) grade++;
			}
			student[i].num=i;  
			student[i].count=grade;
		}
		//调用qsort函数排序 
		qsort(student,n,sizeof(NODE),cmp);
		printf("test case %d:\n",k+1);
		for(int i=0; i < n; i++) {
			printf("Student %d: %d\n",student[i].num,student[i].count);
		}
	}
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值