Rank List(map)

 题意:首先给出一个整数n表示班级的学生人数,然后是学生名字,之后给出一整数m,表示考试次数,下面每行是每次考试学生的分数和名字,最后输出每次考试李明的排名,排名由总分决定。

思路:定义一个map,存入每次考试学生的成绩和名字,由于排名由每次考试的分数总和决定,所以map最后是用分数总和与李明的成绩进行比较,最后输出李明的排名即可,注意要吸收空格。

Li Ming is a good student. He always asks the teacher about his rank in his class after every exam, which makes the teacher very tired. So the teacher gives him the scores of all the student in his class and asked him to get his rank by himself. However, he has so many classmates, and he can’t know his rank easily. So he tends to you for help, can you help him?

Input

The first line of the input contains an integer N (1 <= N <= 10000), which represents the number of student in Li Ming’s class. Then come N lines. Each line contains a name, which has no more than 30 letters. These names represent all the students in Li Ming’s class and you can assume that the names are different from each other.

In (N+2)-th line, you'll get an integer M (1 <= M <= 50), which represents the number of exams. The following M parts each represent an exam. Each exam has N lines. In each line, there is a positive integer S, which is no more then 100, and a name P, which must occur in the name list described above. It means that in this exam student P gains S scores. It’s confirmed that all the names in the name list will appear in an exam.

Output

The output contains M lines. In the i-th line, you should give the rank of Li Ming after the i-th exam. The rank is decided by the total scores. If Li Ming has the same score with others, he will always in front of others in the rank list.

Sample Input

3
Li Ming
A
B
2
49 Li Ming
49 A
48 B
80 A
85 B
83 Li Ming

Sample Output

1
2

AC代码 

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<map>
#include<string> 
using namespace std;
map<string,int>m;
map<string,int>::iterator it;		//迭代器
int main()
{
	int i,n,t,k;
	char s[10010];
	cin>>n;
	getchar();
	for(i=1;i<=n;i++)
	{
		gets(s);
		m[s]=0;				//相当于初始化map数组 
	}
	cin>>t;
	while(t--)
	{
		int num=1;
		for(i=1;i<=n;i++)
		{
			cin>>k;
			getchar();
			gets(s);
			m[s]+=k;		//题目要求是分数总和排名 
		}
		for(it=m.begin();it!=m.end();it++)
		{
			if((*it).second>m["Li Ming"])			//分别让每个同学的成绩和李明比较 
				num++;								//计算李明的排名 
		}
		cout<<num<<endl;
	}
	return 0;
} 

努力努力再努力

Guess you like

Origin blog.csdn.net/zz_xun/article/details/119838605