思路:
设置两个数组,一个存数,另一个存各位数字之和,然后按照各位数字之和的顺序排序,输出
#include <stdio.h>
int main()
{
int num;
while(scanf("%d",&num) && num != 0)
{
int str1[1000],str2[1000]= {0},i,num1,j,temp1,temp2;
for(i = 0;i < num;i++)
{
scanf("%d",&str1[i]);
num1 = str1[i];
while(num1 / 10 != 0 )
{
str2[i] = str2[i] + num1%10;
num1 = num1 /10;
}
str2[i] = str2[i] + num1;
}
for(i = 0;i < num;i++)
for(j = 0;j < num-1-i;j++)
{
if(str2[j] > str2[j+1])
{
temp1 = str1[j];
str1[j] = str1[j+1];
str1[j+1] = temp1;
temp2 = str2[j];
str2[j] = str2[j+1];
str2[j+1] = temp2;
}
}
for(i = 0;i < num-1;i++)
{
printf("%d ",str1[i]);
}
printf("%d\n",str1[num-1]);
}
return 0;
}