hdu 1718 Rank

本文详细解析了一种用于计算学生排名的算法,该算法通过读取学生的唯一编号和分数,对比特定学生(如Jackson)的分数,从而确定其在班级中的排名。文章提供了一个C++代码示例,展示了如何使用标准模板库(map)来实现这一功能。

题目连接

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 }
View Code

 

转载于:https://www.cnblogs.com/GadyPu/p/4563512.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值