排序检索-10194-football

题目大意:给出每场比赛的得分情况,统计参赛队伍的排名。具体排名规则不再赘述。

解题过程:WA无数次。主要原因:1、排序规则不是一味按照从大到小排的,如最少比赛数就是从小到大排的;2、队名并不是完全asc码排序的,而是把队名转成小写再排序的,这是我看别人的结题报告才发现的。我觉得这是UVA本身的Bug,因为我做的另一道UVA题就是完全按照ASC码排序的,表述却都是“lexographic order”。

注:再次复习了scanf和printf的用法,对于格式统一的输入输出来说,的确很方便

scanf("%[^\n]%*c",teams[i].name);//读一行
scanf("%d\n",&g);//换行符一定要加上,否则下一个读入会出错
scanf("%[^#@]#%d@%d#%[^\n#@]%*c",&t1,&g1,&g2,&t2);//标志字符分隔


</pre><p></p><p>正确代码:</p><pre name="code" class="cpp"># include <cstdio>
# include <cstdlib>
# include <cstring>
# include <ctype.h>
# include <string>
//# define LOCAL
using namespace std;
struct team{
	char name[50];
	char qname[50];
	int allpoints,games,gamevic,gametie,gamelos,goaldife,goalsco,goalagt;
	team(){
		strcpy(name,"");
		allpoints=games=gamevic=gametie=gamelos=goaldife=goalsco=goalagt=0;
	}
	void clean(){
		strcpy(name,"");
		allpoints=games=gamevic=gametie=gamelos=goaldife=goalsco=goalagt=0;
	}
	void update(char a,int h,int i){
		if(a=='w') gamevic++;
		else if(a=='l') gamelos++;
		else gametie++; 
		goalsco+=h;
		goalagt+=i;
	}
	void summrize(){
		for(int i=0;i<strlen(name);i++){
			qname[i]=tolower(name[i]);
		}
		qname[strlen(name)]='\0';
		allpoints=gamevic*3+gametie;
		games=gamevic+gametie+gamelos;
		goaldife=goalsco-goalagt;
	}
	void print(int i){
		printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n",i+1,name,allpoints,games,gamevic,gametie,gamelos,goaldife,goalsco,goalagt);
	}
};
char nameoftour[100];
int cmp(const void*a,const void*b){
	team ta=*(team*)a;
	team tb=*(team*)b;
	if(ta.allpoints!=tb.allpoints) return tb.allpoints-ta.allpoints;
	if(ta.gamevic!=tb.gamevic) return tb.gamevic-ta.gamevic;
	if(ta.goaldife!=tb.goaldife) return tb.goaldife-ta.goaldife;
	if(ta.goalsco!=tb.goalsco) return tb.goalsco-ta.goalsco;
	if(ta.games!=tb.games) return ta.games-tb.games; 
	return strcmp(ta.qname,tb.qname);
}
team teams[1000];
int main(){
	# ifdef LOCAL
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
	#endif
	int n;
	scanf("%d\n",&n);
	while(n){
		n--;
		scanf("%[^\n]%*c",nameoftour);//name of tournament
//		printf("name is %s\n",nameoftour);
		int t,g;
		scanf("%d\n",&t);
		for(int i=0;i<t;i++){
			teams[i].clean();
			scanf("%[^\n]%*c",teams[i].name);
//			printf("%s\n",teams[i].name);
		}
		scanf("%d\n",&g);
		int g1,g2;
		char t1[50],t2[50];
		int winner;
		int pos1,pos2;
		for(int i=0;i<g;i++){
			scanf("%[^#@]#%d@%d#%[^\n#@]%*c",&t1,&g1,&g2,&t2);
//			printf("%s:%d %s:%d\n",t1,g1,t2,g2);
			for(int j=0;j<t;j++){
				if(strcmp(teams[j].name,t1)==0){
					pos1=j;
					break;
				}
			}
			for(int j=0;j<t;j++){
				if(strcmp(teams[j].name,t2)==0){
					pos2=j;
					break;
				}
			}
			if(g1>g2){
				teams[pos1].update('w',g1,g2);
				teams[pos2].update('l',g2,g1);
			}
			else if(g1<g2){
				teams[pos1].update('l',g1,g2);
				teams[pos2].update('w',g2,g1);
			}
			else{
				teams[pos1].update('t',g1,g2);
				teams[pos2].update('t',g2,g1);
			}
		}
		for(int i=0;i<t;i++){
			teams[i].summrize();
		}
		qsort(teams,t,sizeof(teams[0]),cmp);
		printf("%s\n",nameoftour);
		for(int i=0;i<t;i++){
			teams[i].print(i);
		}
		if(n) printf("\n");
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值