您的位置:首页 > 理论基础 > 数据结构算法

数据结构基础+STL--winner map

2013-04-05 18:27 281 查看
Description
The winner ofthe card game popular in Berland "Berlogging" is determined accordingto the following rules. If at the end of the game there is only one player withthe maximum number of points, he is the winner. The
situation becomes more difficultif the number of such players is more than one. During each round a playergains or loses a particular number of points. In the course of the game thenumber of points is registered in the line "name score",where name is a player's
name, and score is the number ofpoints 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 playershave the maximum number of points (say, it equals to m)
at the end ofthe game, than wins the one of them who scored at least m pointsfirst. Initially each player has 0 points. It's guaranteed that at the end ofthe game at least one player
has a positive number of points.
Input
The first linecontains an integer number n (1  ≤  n  ≤  1000), n is
thenumber of rounds played. Then follow n lines, containing the informationabout the rounds in "name score" format in chronological order,where name is a string of lower-case Latin
letters with the lengthfrom 1 to 32, and score is an integer number between -1000 and 1000,inclusive.
Output
Print the nameof the winner.
Sample Input
Input
3

mike 3

andrew 5

mike 2
Output
andrew
Input
3

andrew 3

andrew 2

mike 5
Output
andrew
利用哈希记录数据,但是要注意题目的题意。最后的赢家既要有最高的积分,有得是最先达到高于或者等于最高积分的选手。

#include<map>

#include<iostream>

#include<string>

#include<cstdio>

using namespace std;

char name[1001][35];

int score[1001];

int main()

{

         map<string,int>m;

         map<string,int>mm;

         stringnname;

         intn,i,max;

         int temp;

         while(cin>>n)

         {

                   getchar();

                   m.clear();

                   mm.clear();              

                   max=-99999;

                   for(i=0;i<n;i++)

                   {

                            cin>>name[i];

         cin>>score[i];

         m[name[i]]+=score[i];

         mm[name[i]]=0;

                   }

                   map<string,int>::iteratorp;

                   temp=0;

                   for(p=m.begin();p!=m.end();p++)

                   {

                            if(m[p->first]>max)           

                                     max=m[p->first];

                   }

                   for(p=m.begin();p!=m.end();p++)

                   {

                            if(m[p->first]==max)

                                     {

                                               temp++;

                                               nname=p->first;

                            }

                   }

                   if(temp==1)

                            cout<<nname<<endl;

                   else

                   {

                   for(i=0;i<n;i++)

                   {

                            mm[name[i]]+=score[i];

                            if(mm[name[i]]>=max&&m[name[i]]==max)

                            {

                                     cout<<name[i]<<endl;

                                     break;

                            }

                   }

                   }

         }

         return0;

 

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: