首页 > 代码库 > cf------(round 2)A. Winner
cf------(round 2)A. Winner
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) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It‘s guaranteed that at the end of the game at least one player has a positive number of points.
The first line contains an integer number n (1 ≤ n ≤ 1000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in "name score" format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive.
Print the name of the winner.
3
mike 3
andrew 5
mike 2
andrew
3
andrew 3
andrew 2
mike 5
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 }