1028 按长度排序

Description:

有一些整数,它的结构是:第一行为一个整数N,表示后面有N个整数需要排序输出,先按长度排,如长度一样则按大小排,若遇到N等于零,则运行结束。

Sample Input:

3
123
12
3333
2
1000000000000000
1
0

Sample Output:

12
123
3333

1
1000000000000000

source:
#include <iostream>
using namespace std;
bool Compare(char* pFirst,char* pLast)
{
	int len1=strlen(pFirst),len2=strlen(pLast);
	if(len1!=len2)
		return len1>len2;
	char *pChar1=pFirst,*pChar2=pLast;
	for(;*pChar1!='0';++pChar1,++pChar2)
		if(*pChar1!=*pChar2)
			return *pChar1>*pChar2;
	return false;
}
void sort(char** nums,int len)
{
	int index=0;
	bool move=true;
	char* temp=NULL;
	while(move)
	{
		move=false;
		for(index=0;index<len-1;++index)
			if(Compare(nums[index],nums[index+1]))
			{
				move=true;
				temp=nums[index];
				nums[index]=nums[index+1];
				nums[index+1]=temp;
			}
	}
}
int main(int argc, char* argv[])
{
	int num=0;
	while(cin>>num&&num)
	{
		int index=0;
		char** input=new char*[num];
		for(;index<num;++index)
		{
			char* buf=new char[255];
			cin>>buf;
			input[index]=buf;
		}
		sort(input,num);
		for(index=0;index<num;++index)
		{
			cout<<input[index]<<endl;
			delete input[index];
		}
		delete input;
		cout<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值