Description
给定n个正整数,根据各位数字之和从小到大进行排序。
Input
输入数据有多组,每组数据占一行,每行的第一个数正整数n,表示整数个数,后面接n个正整数。当n为0时,不作任何处理,输入结束。n<=10
Output
输出每组排序的结果。
Sample
Input
3 230 59 110
5 199 220 108 235 120
0
Output
110 230 59
120 220 108 235 199
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <stdlib.h>
using namespace std;
int n;
int a[11],b[11];
int main()
{
while(cin>>n)
{
if(n==0)
{
break;
}
for(int i=1; i<=n; i++)
{
int sum=0;
scanf("%d",&a[i]);
int x=a[i];
while(x)
{
int t;
t=x%10;
sum+=t;
x=x/10;
}
b[i]=sum;
}
for(int i=1; i<n; i++)
{
for(int j=i+1;j<=n;j++)
{
if(b[i]>b[j])
{
swap(b[i],b[j]);
swap(a[i],a[j]);
}
}
}
for(int i=1; i<n; i++)
{
printf("%d ",a[i]);
}
printf("%d\n",a[n]);
}
return 0;
}