cf------(round 2)A. Winner
A. Winner
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output
The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line "name score", where name is a player's name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m
Input
The first line contains an integer number n (1 ≤ n ≤ 1000), n is the number of rounds played. Then follow n
Output
Print the name of the winner.
Sample test(s)
Input
3 mike 3 andrew 5 mike 2
Output
andrew
Input
3
andrew 3
andrew 2
mike 5
Output
andrew
此題不大好懂,就是要你求出最大分數而且最先得到這個最高分的那個人的名字
思路: 第一步: 我們先求出所有人的最終得分
第二步: 我們求出最終得分的最高分:
第三部: 我們求出第一個得到最高分,而且最終得分也是最高分的那個人,那必定是最先出現的那個人.....
程式碼:
1 #include<iostream>
2 #include<map>
3 using namespace std;
4 struct node
5 {
6 int score;
7 string name;
8 }ss[1005];
9 int main(){
10
11 string winner;
12 int n,maxsc;
13 map<string ,int>str;
14 //freopen("test.in","r",stdin);
15 while(cin>>n){
16 if(!str.empty()) str.clear();
17 for(int i=0;i<n;i++){ //求出最終態勢
18 cin>>ss[i].name>>ss[i].score;
19 str[ss[i].name]+=ss[i].score;
20 }
21 //求出最大的值
22 maxsc=str[ss[0].name];
23 for(int i=1;i<n;i++){
24 if(maxsc<str[ss[i].name]){
25 maxsc=str[ss[i].name];
26 }
27 }
28 map<string ,int>check; //這個表示過程態勢
29 for(int i=0;i<n;i++){
30 check[ss[i].name]+=ss[i].score;
31 if(check[ss[i].name]>=maxsc&&str[ss[i].name]>=maxsc){
32 winner=ss[i].name;
33 break;
34 }
35 }
36 cout<<winner<<endl;
37 }
38 return 0;
39 }