Description
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", wherename 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 tom) at the end of the game, than wins the oneof 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.
Input
The first line contains an integer number n (1 ≤ n ≤ 1000),n is the number of rounds played. Then follown lines, containing the information about the rounds in "name score" format in chronological order, wherename is a string of lower-case Latin letters with the length from 1 to 32, andscore is an integer number between -1000 and 1000, inclusive.
Output
Print the name of the winner.
Sample Input
3 mike 3 andrew 5 mike 2
andrew
3 andrew 3 andrew 2 mike 5
andrew
题意:输入名字和他们所持有的点数 先达到最大者为获胜者 最后输出获胜者的名字
解题:原来做过一道关于dfs的题 里面的将字符串变为数组下标印象深刻 所以开始的时候也是将其转换为数组下标 但是没有考虑到如果输入的是负数这种情况
所以后面坐了好多改动才过~ 其实我开始也不知道怎么过的~ 开始wa然后就过了 然后又wa了 然后又过了 wokao~
下面的代码稳过:
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char name[1000][1000];
char str[1000];
int xiabiao[1000];
int shuzhi[1000];
int m;
int q[10000];
int num;
int k;
int match(char *str) //就是这一步一直没有过~ 爷爷的~
{
for(int i=0;i<m;++i)
if(!strcmp(str,name[i]))return i;
q[m]=0;
strcpy(name[m],str);
return m++;
}
int main()
{
int cas;
while(cin>>cas)
{
m=0;
int sub;
for(int i=0;i<cas;i++)
{
cin>>str>>num;
sub=match(str);
q[sub]+=num;
xiabiao[k]=sub;//将下标和数值分别放在一个数组里
shuzhi[k]=q[sub];
k++;
}
int temp=0; //找到最大的值
for(int i=0;i<m;i++)
{
if(temp<q[i])
temp=q[i];
}
int d=0;
for(int i=k;i>0;i--) //倒着查找 最后输出最大的下标
{
if(shuzhi[i]>=temp&&q[xiabiao[i]]==temp)
d=xiabiao[i];
}
cout<<name[d]<<endl;
}
return 0;
}
最后贴出刚开始的时候只考虑是正输入的代码,虽然错了 我觉得当时出来的时候还是挺有感觉的 对以后算做警示吧~
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string.h>
using namespace std;
int cn;
char name[1000][1000];
int q[10000];
int match(char c[])
{
int i;
for ( i=1;i<=cn;i++)
if (strcmp(name[i],c)==0)
return i;
strcpy(name[i-1],c);
cn++;
return i-1;
}
int main()
{
int cas;
int num;
int a;
while(cin>>cas)
{
num=0;
memset(q,0,sizeof(q));
int temp=-1;
int k=1;
for(int i=1;i<=cas;i++)
{
cin>>name[i]>>num;
a=match(name[i]);
q[a]=num+q[a];
if(q[a]>temp)
{
temp=q[a];
k=a;
}
}
cout<<name[k]<<endl;
}
}