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;
}