题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=1718
Rank
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
20070101
20070102 100
20070101 33
20070103 22
20070106 33
0 0
Sample Output
2
stl大法好。。
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<map> 7 using std::map; 8 map<int, int> rec; 9 int main() { 10 #ifdef LOCAL 11 freopen("in.txt", "r", stdin); 12 freopen("out.txt", "w+", stdout); 13 #endif 14 int num, id, score, rank, targe; 15 while (~scanf("%d", &num)) { 16 rank = 0, rec.clear(); 17 while (~scanf("%d %d", &id, &score) && id + score) rec[id] = score; 18 targe = rec[num]; 19 map<int, int>::iterator ite; 20 for (ite = rec.begin(); ite != rec.end(); ++ite) { 21 if (ite->second > targe) rank++; 22 } 23 printf("%d\n", rank + 1); 24 } 25 return 0; 26 }
本文详细解析了一种用于计算学生排名的算法,该算法通过读取学生的唯一编号和分数,对比特定学生(如Jackson)的分数,从而确定其在班级中的排名。文章提供了一个C++代码示例,展示了如何使用标准模板库(map)来实现这一功能。
1万+

被折叠的 条评论
为什么被折叠?



