数据结构基础+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;
}
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;
}
相关文章推荐
- STL之map、set数据结构基础
- c++ 数据结构之STL之 map
- Python基础,基本类型(整型,浮点数等)数据结构(List,dic(Map),Set,Tuple),控制语句(if,for,while,continue or break):来自学习资料
- 数据结构基础+STL--sequence
- 数据结构基础+STL--tree recovery
- 数据结构基础+STL--第k大
- 数据结构基础+STL--符号匹配
- 数据结构基础+STL--字典翻译
- 数据结构基础-队列(循环队列、链式队列)以及STL中queue的使用
- 从零开始_学_数据结构(五)——STL(map、set、list、vector)
- 步步为营(十一)常用数据结构(4)STL-Map(应该是散列表)
- 数据结构基础1:线性表(1)
- 数据结构基础之双向链表(约瑟夫问题)
- 数据结构基础(8) --单链表的设计与实现(1)之基本操作
- c语言基础表达式, 关系运算符, 逻辑运算符, 位运算符, 数据的取值范围, 分支结构(if...else, switch...case)
- 基础数据结构之队列
- C++存储数据结构之四map
- 数据结构基础(3)-->队列
- 数据结构基础(22)--红黑树的设计与实现(上)
- 数据结构基础7.4:归并排序