2013
12-21

# 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;
}

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

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