hdu 2115 I Love This Game

这题其实就是一个简单的排序,但某渣太差了,代码写了很长,字典序排序也是一个一个字符去比较

看了别人代码才发现其实用strcmp 就可以了

自己代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<limits.h>
using namespace std;
struct node
{
	int m;
	int s;
	char ch[111];
}list[15];
int cmp(node x,node y)
{
	if(x.m < y.m)
		return 1;
	else if(x.m > y.m)
		return 0;
	else if(x.m==y.m)
	{
		if(x.s < y.s)
			return 1;
		else if(x.s > y.s)
			return 0;
		else if(x.s==y.s)
		{
			int l1= strlen(x.ch);
			int l2= strlen(y.ch);
			int min= l1 < l2? l1: l2;
			for(int i= 0; i< min; i++)
			{	
				if(x.ch[i]< y.ch[i])
					return 1;
				else if(x.ch[i]> y.ch[i])
					return 0;
			}
			if(l1 < l2)
				return 1;
			else
				return 0;
		}		
	}			
}
int main()
{
	int n;
	int T= 0;
	while(scanf("%d",&n)!=EOF&&n)
	{
		T++;
		for(int i= 1; i<= n; i++)
			scanf("%s %d:%d",&list[i].ch,&list[i].m,&list[i].s);
		sort(list+1,list+n+1,cmp);
		if(T!=1)
			printf("\n");
		printf("Case #%d\n",T);
		printf("%s 1\n",list[1].ch);
		int flag= 1;
		for(int i= 2; i<= n; i++)
		{
			if(list[i].m==list[i-1].m&&list[i].s==list[i-1].s)
				printf("%s %d\n",list[i].ch,flag);
			else
			{	
				printf("%s %d\n",list[i].ch,i);	
				flag= i;
			}
		}
	}
	return 0;
}


别人写的cmp:

int cmp(p a,p b)
{
    if(a.m!=b.m)
        return a.m<b.m;
    else if(a.s!=b.s)
        return a.s<b.s;
    else
        return strcmp(a.name,b.name)<0;
}


 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值