Rank 1718

来源:互联网 发布:visual foxpro和sql 编辑:程序博客网 时间:2024/04/29 05:18

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

AC代码

#include <cstdio>#include <algorithm>#include <vector>#include <string>#include <iostream>struct student{    std::string m_szID;    int m_nScore;};class cmp{public:    bool operator()(const student& lhs, const student& rhs)    {        return lhs.m_nScore > rhs.m_nScore;    }};int main(int argc, const char* argv[]){    std::string szFind;    while(std::cin >> szFind)    {        std::vector<student> vec;        student stu;        while (std::cin >> stu.m_szID >> stu.m_nScore && !(stu.m_szID  == "0" && stu.m_nScore == 0))        {            vec.push_back(stu);        }        std::sort(vec.begin(), vec.end(), cmp());        int nCount = 0, nPre = -1;        for (unsigned i=0; i<vec.size(); ++i)        {            if (nPre == vec[i].m_nScore)            {                ++nCount;            }            else            {                nPre = vec[i].m_nScore;                nCount = 0;            }            if (vec[i].m_szID == szFind)            {                printf("%d\n", i+1-nCount);                break;            }        }    }    return 0;}
0 0