解题排行榜中,按解题总数生成排行榜。假设每个学生信息仅包括学号、解题总数;要求先输入n个学生的信息;然后按“解题总数”降序排列,若“解题总数”相同则按“学号”升序排列。 输入格式: 首先输入一个正整数T,表示测试数据的组数,然后是T组测试数据。 每组测试数据先输入1个正整数n(1 ≤ n ≤ 100),表示学生总数。然后输入n行,每行包括1个不含空格的字符串s(不超过8位)和1个正整数d,分别表示一个学生的学号和解题总数。 输出格式: 对于每组测试数据,输出最终排名信息,每行一个学生的信息:排名、学号、解题总数。每行数据之间留一个空格。注意,解题总数相同的学生其排名也相同。 输入样例: 1 4 0010 200 1000 110 0001 200 0100 225 输出样例: 1 0100 225 2 0001 200 2 0010 200 4 1000 110
时间: 2024-02-15 19:05:40 浏览: 9
该问题可以使用结构体来存储学生信息,然后使用qsort函数进行排序。具体实现如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char id[9];
int score;
int rank;
} Student;
int cmp(const void *a, const void *b) {
Student *p1 = (Student *)a;
Student *p2 = (Student *)b;
if (p1->score != p2->score) {
return p2->score - p1->score;
} else {
return strcmp(p1->id, p2->id);
}
}
int main() {
int T;
scanf("%d", &T);
for (int i = 0; i < T; i++) {
int n;
scanf("%d", &n);
Student students[n];
for (int j = 0; j < n; j++) {
scanf("%s %d", students[j].id, &students[j].score);
}
qsort(students, n, sizeof(Student), cmp);
int rank = 1;
for (int j = 0; j < n; j++) {
if (j > 0 && students[j].score != students[j-1].score) {
rank = j + 1;
}
students[j].rank = rank;
printf("%d %s %d\n", students[j].rank, students[j].id, students[j].score);
}
}
return 0;
}
```
程序中,结构体Student中包含了学号id、解题总数score和排名rank三个成员变量。排序函数cmp中,首先按照解题总数score进行降序排列,如果解题总数score相同,再按照学号id进行升序排列。在排序完毕后,根据排名和成员变量进行输出操作。
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)