这是codeforce上的2A,被这道题折磨了很长一段时间,想想就很不爽。。。。
关于这道题目,更详细的地方看这里: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 oneof them who scored at leastm 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.
题目的大致要求是,首先输入一个n代表play的次数,可以理解为游戏进行了几个回合;然后根据n,依次输入n组数据,每组数据的组成:名字和他在这次回合中取得的分数,譬如像:mike 3,这其中的mike是名字,而3代表他在这次回合中取得的分数(分数可以为负);这便是输入的要求;
接着来谈谈这个里面最让人头痛的,也就是他的赢的方式:如果只有一个人取得最高分,那么最后赢的就是分数最高的;但假如取得最高分的有多个人是一样的,那么胜者应该是这里面最先取得不小于最高分的人并且这个人是最后取得最高分的多个人中的一个。
分析它的过程
下面在来详细的讨论下做法:(我是用的是java)
这是存储每个用户的结构:
class Person
{
int id; //用户的编号
String name; //用户的名字
int score; //用户的分数
// int flag = 0;
}
用来存储胜者的信息:
Person pLast = new Person();
pLast.id = -1;
pLast.name = "";
pLast.score = max;
使用al来记录玩游戏的次数与玩的详细信息,并且加入相同的名字的时候要进行替代
使用bl,同al的内容差不多,但是每玩一次都要记录,尽管名字相同,在bl中不存在删除;
ArrayList<Person> al = new ArrayList<Person>();
ArrayList<Person> bl = new ArrayList<Person>();
接着是实现al与bl:
for(int i = 0; i < gametime; i++)
{
Person p = new Person();
p.id = number++;
p.name = in.next();
p.score = in.nextInt();
Iterator<Person> iterator = al.iterator();
while(iterator.hasNext())
{
Person pTest = iterator.next();
if(p.name.equals(pTest.name))
{
p.score = p.score + pTest.score;
iterator.remove();
}
}
al.add(p);
bl.add(p);
}
取得最高分,并通过最高分检查是否有多个用户的最高分数是一样的:
Iterator<Person> iterator = al.iterator();
while(iterator.hasNext())
{
Person pTest = iterator.next();
// System.out.println(pTest.id+" "+pTest.name+" "+pTest.score);
if(pTest.score > pLast.score)
{
pLast = pTest;
}
}
Iterator<Person> iteratorFlag = al.iterator();
while(iteratorFlag.hasNext())
{
Person pTest = iteratorFlag.next();
if(!pTest.name.equals(pLast.name) && pTest.score == pLast.score)
{
flag = true;
// System.out.println(flag+"aaaa");
}
}
通过flag变量来判断是哪一种赢得方式:是多人同最高分,还是只有一人:
if(flag)
{
ArrayList<String> name = new ArrayList<String>();
Iterator<Person> iteratorA = al.iterator();
while(iteratorA.hasNext())
{
Person pTest = iteratorA.next();
if(pTest.score == pLast.score)
{
name.add(pTest.name);
}
}
Iterator<Person> iteratorB = bl.iterator();
while(iteratorB.hasNext())
{
Person pTest = iteratorB.next();
Iterator<String> iteratorN = name.iterator();
while (iteratorN.hasNext())
{
String strName = iteratorN.next();
if (strName.equals(pTest.name) && pTest.score >= pLast.score)
{
System.out.println(pTest.name);
return;
}
}
}
}
System.out.println(pLast.name);
好了,基本过程就是这些了,本来打算还讲点的,但我发现这已经很长了,简单点说就好
(注:如果看到图不爽的话就不要看了,图画的确实菜)