程序设计入门16 排序和排名与结构体
发布日期:2021-05-08 12:50:54 浏览次数:21 分类:精选文章

本文共 2188 字,大约阅读时间需要 7 分钟。

1025 PAT Ranking (25)(25 分)

Programming Ability Test (PAT) is organized by the College of ComputerScience and Technology of Zhejiang University. Each test is supposed torun simultaneously in several places, and the ranklists will be mergedimmediately after the test. Now it is your job to write a program tocorrectly merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first linecontains a positive number N (<=100), the number of test locations.Then N ranklists follow, each starts with a line containing a positiveinteger K (<=300), the number of testees, and then K lines containingthe registration number (a 13-digit number) and the total score of eachtestee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees.Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted innondecreasing order of the final ranks. The testees with the same scoremust have the same rank, and the output must be sorted in nondecreasingorder of their registration numbers.

Sample Input:

251234567890001 951234567890005 1001234567890003 951234567890002 771234567890004 8541234567890013 651234567890011 251234567890014 1001234567890012 85

Sample Output:

91234567890005 1 1 11234567890014 1 2 11234567890001 3 1 21234567890003 3 1 21234567890004 5 1 41234567890012 5 2 21234567890002 7 1 51234567890013 8 2 31234567890011 9 2 4

正确答案(心累!!!):

1,sort传的是指针。

2,那些下标之间的关系搞清楚。

3,能用for语句里面的变量表示,尽量用它,开很多新的变量会很乱。

#include
#include
#include
using namespace std;struct Student{ char id[15]; int location; int local_rank; int score;}stu[30010];bool cmp(Student a,Student b){ if(a.score!=b.score) return a.score>b.score; else return strcmp(a.id,b.id)<0;}int main(){ int n,m,num=0; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&m); for(int j=0;j
0&&stu[i].score!=stu[i-1].score){ r = i + 1; } printf("%s %d %d %d\n",stu[i].id,r,stu[i].location,stu[i].local_rank); } return 0;}

 

上一篇:程序设计入门17 数组A+B Format
下一篇:程序设计入门15 日期累加(几个输入憋着输出,还是一个一个地输出都A了!!!)

发表评论

最新留言

感谢大佬
[***.8.128.20]2025年04月11日 22时15分17秒