题意:世界杯足球比赛,输入n个足球队。然后每个足球队互相一场比赛,赢了的队+3分,平局两支队各加一分,输了不加分。总共进行n*(n-1)场比赛,经过淘汰赛,淘汰一半队伍,然我输出最后留下的队伍名字。****按字典序输出留下的队伍****。
题解:模拟,用map模拟比赛的过程,把对应的分数加上,但是这是遇到最麻烦的A题,没有之一!本来只需要用map记录每个队伍的得分就行的,然后哦排序,分数从高到底排。就能输出留下的队伍,但是!!!对留下的队伍字典序排序,你以为很简单吗?把留下的队伍在排一次序?我刚开始就这么整的,但是wa第四组,
4TeMnHVvWKpwlpubwyhzqvc
AWJwc
bhbxErlydiwtoxy
EVASMeLpfqwjkke
AWJwc-TeMnHVvWKpwlpubwyhzqvc 37:34
bhbxErlydiwtoxy-TeMnHVvWKpwlpubwyhzqvc 38:99
bhbxErlydiwtoxy-AWJwc 33:84
EVASMeLpfqwjkke-TeMnHVvWKpwlpubwyhzqvc 79:34
EVASMeLpfqwjkke-AWJwc 24:37
EVASMeLpfqwjkke-bhbxErlydiwtoxy 3:6
4组数据留两个队伍,第一名 AWJwc最高分没问题,字典序也是第一个输出,但是TeMnHVvWKpwlpubwyhzqvc和EVASMeLpfqwjkke队伍得分相同。而在第一次排排序的时候,按照的是先后顺序,也就是AWJwc,TeMnHVvWKpwlpubwyhzqvc,EVASMeLpfqwjkke........这样排的,第二次按照字典序排序,输出两个的时候,不会去管E那个队伍,如果你想按照字典序全部排的话,那样又有问题了,第一次排的大小等于没排,只是按照字典序排,输出的就不是应该留下的队伍了。所以要map,用三个,分别记录,每个队伍的得分,还要记录,每个队伍得到的总分,每个队伍的每场比赛的差分。来判断....参考别人代码发现的....还有点懵
#include<bits/stdc++.h>
using namespace std;
map<string,int>m,g,gm;
bool cmp(string s1,string s2)
{
if(m[s1]!=m[s2])
return m[s1]>m[s2];
if(gm[s1]!=gm[s2])
return gm[s1]>gm[s2];
return g[s1]>g[s2];
}
int main()
{
string s,s1,s2,name[55];
char c;
int n,g1,g2;
cin>>n;
for(int i=0; i<n; i++)
cin>>name[i];
for(int i=0; i<n*(n-1)/2; i++)
{
cin>>s>>g1>>c>>g2;
s1=s.substr(0,s.find("-"));///substr分割从0开始到'-'的字符串表示队伍s1
s2=s.substr(s.find("-")+1);///分给从'-'下一位开始到字符串结束的字符串表示队伍s2
if(g1==g2)
{
m[s1]++; ///两个分数相等,平局各加1分
m[s2]++;
}
else m[g1>g2?s1:s2]+=3;///三目运算,取分数大的队伍,加+3分,输的队伍不得分
g[s1]+=g1,g[s2]+=g2; ///记录每个队伍总分
gm[s1]+=g1-g2,gm[s2]+=g2-g1;///记录每个队伍比赛的差分
}
sort(name,name+n,cmp);///留下的队排序
sort(name,name+n/2); ///字典排序
for(int i=0; i<n/2; i++)
cout<<name[i]<<endl;
return 0;
}