首页 > ACM题库 > HDU-杭电 > HDU 1718 Rank-模拟[解题报告] C++
2013
12-21

HDU 1718 Rank-模拟[解题报告] C++

Rank

问题描述 :

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.

输入:

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.

输出:

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

样例输入:

20070101
20070102 100
20070101 33
20070103 22
20070106 33
0 0

样例输出:

2

#include<stdio.h>
#include<math.h>
#include <queue>
#include<algorithm>
#include <iostream>
#include <string.h>
using namespace std;

int num[1001];

int main()
{
    int dir;
    while(~scanf("%d",&dir))
    {
        memset(num,0,sizeof(num));
        int a,b,score=-1,t=0,ans=0;
        while(~scanf("%d%d",&a,&b))
        {
            if(a==0&&b==0)break;
            if(score!=-1)
            {
                if(b>score)ans++;
            }
            else
            {
                if(a==dir)score=b;
                else num[t++]=b;
            }
        }
        for(int i=0;i<t;i++)
        {
            if(num[i]>score)ans++;
        }
        printf("%d\n",ans+1);
    }
    return 0;
}

解题报告转自:http://blog.csdn.net/candy20094369/article/details/6845331


  1. 您没有考虑 树的根节点是负数的情况, 若树的根节点是个很大的负数,那么就要考虑过不过另外一边子树了

  2. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮