ACMSTEP 1.3.8 Rank //水题 排序

来源:互联网 发布:narcissu知乎 编辑:程序博客网 时间:2024/06/15 11:19

原题链接


Rank

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 821 Accepted Submission(s): 271
Problem Description
Jackson wants to know his rank in the class. The professor has posted a list of student numbers and marks. Compute Jackson’s rank in class; that is, if he has the top mark(or is tied for the top mark) his rank is 1; if he has the second best mark(or is tied) his rank is 2, and so on.

Input
The input consist of several test cases. Each case begins with the student number of Jackson, an integer between 10000000 and 99999999. Following the student number are several lines, each containing a student number between 10000000 and 99999999 and a mark between 0 and 100. A line with a student number and mark of 0 terminates each test case. There are no more than 1000 students in the class, and each has a unique student number.

Output
For each test case, output a line giving Jackson’s rank in the class.

Sample Input
2007010120070102 10020070101 3320070103 2220070106 330 0

Sample Output
2


一开始没有弄清楚题意就做,搞得乱七八糟,WA了两次。反复读题 重新写了一遍 AC。第一次用结构体做的。后来发现不需要,而且此题的题意理解和常规的 成绩排序有点不大一样。


#include <cstdio>#include <cstdlib>using namespace std;int cmp(void const *x,void const *y){ return *((int *)y)-*((int *)x);}int main(){int n,num[1001];int grade[1001],count,i,j,temp;while(scanf("%d",&n)!=EOF){for(i=0;;i++){scanf("%d%d",&num[i],&grade[i]);if(num[i]==n)temp = grade[i];if( !num[i] && !grade[i])break;}qsort(grade,i,sizeof(grade[0]),cmp);count=0;for(j=0;j<i;j++){if(grade[j]>temp||grade[j]==temp)count++;if(grade[j]==temp)break;}printf("%d\n",count);}}