描述
将01串首先按长度排序,长度相同时,按1的个数多少进行排序,1的个数相同时再按ASCII码值排序。
输入
输入数据中含有一些01串,01串的长度不大于256个字符。
输出
重新排列01串的顺序。使得串按基本描述的方式排序。
样例输入
10011111
00001101
1010101
1
0
1100
样例输出
0
1
1100
1010101
00001101
10011111
#include<iostream>
using namespace std;
bool f(char *p1,char *p2)
{
int len1,num1,num2,i;
num1=num2=0;
if(strlen(p1)>strlen(p2))
return true;
else
if(strlen(p1)<strlen(p2))
return false;
else
{
len1=strlen(p1);
for(i=0;i<len1;i++)
{
num1+=p1[i]-'0';
num2+=p2[i]-'0';
}
if(num1>num2)
return true;
else
{
if(num1<num2)
return false;
else
{
if(strcmp(p1,p2)<0)
return true;
else
return false;
}
}
}
}
int main()
{
int i=0;
int j,n;
char temp[280];
char str[2000][280];
while(scanf("%s",&str[i])!=EOF)
{
i++;
}
n=i;
for(i=n-1;i>=0;i--)
{
for(j=0;j<i;j++)
{
if(f(str[j],str[j+1]))
{
strcpy(temp,str[j]);
strcpy(str[j],str[j+1]);
strcpy(str[j+1],temp);
}
}
}
for(i=0;i<n;i++)
{
cout<<str[i]<<endl;
}
return 0;
}