Print? Description
Given n positive integers, sort them in ascending order based on the sum of the numbers.
Input
There are multiple groups of input data, each of which occupies one row. The first number of each row is a positive integer (n <= 20), indicating the number of integers followed by n positive integers. If n is 0, no processing is performed and the input ends.
Output
Output the result of sorting each group.
Sample Input
2 1 2
3 121 10 111
0
Sample Output
1 2
10 111 121
Description
Given n positive integers, sort them in ascending order based on the sum of the numbers.
Input
There are multiple groups of input data, each of which occupies one row. The first number of each row is a positive integer (n <= 20), indicating the number of integers followed by n positive integers. If n is 0, no processing is performed and the input ends.
Output
Output the result of sorting each group.
Sample Input
2 1 2
3 121 10 111
0
Sample Output
1 2
10 111 121
[Plain] # include <stdio. h>
Int get (int num );
Int main ()
{
Int j, I, n, a [21], c;
While (1)
{
Scanf ("% d", & n );
If (n = 0)
{
Break;
}
For (I = 0; I <n; I ++)
{
Scanf ("% d", & a [I]);
}
For (I = 0; I <n-1; I ++)
{
For (j = I + 1; j <n; j ++)
{
If (get (a [I])> get (a [j])
{
C = a [I];
A [I] = a [j];
A [j] = c;
}
}
}
For (I = 0; I <n; I ++)
{
Printf ("% d", a [I]);
If (I <n-1)
{
Printf ("");
}
}
Printf ("\ n ");
}
Return 0;
}
Int get (int num)
{
Int sum = 0;
While (num)
{
Sum + = num % 10;
Num/= 10;
}
Return sum;
}
# Include <stdio. h>
Int get (int num );
Int main ()
{
Int j, I, n, a [21], c;
While (1)
{
Scanf ("% d", & n );
If (n = 0)
{
Break;
}
For (I = 0; I <n; I ++)
{
Scanf ("% d", & a [I]);
}
For (I = 0; I <n-1; I ++)
{
For (j = I + 1; j <n; j ++)
{
If (get (a [I])> get (a [j])
{
C = a [I];
A [I] = a [j];
A [j] = c;
}
}
}
For (I = 0; I <n; I ++)
{
Printf ("% d", a [I]);
If (I <n-1)
{
Printf ("");
}
}
Printf ("\ n ");
}
Return 0;
}
Int get (int num)
{
Int sum = 0;
While (num)
{
Sum + = num % 10;
Num/= 10;
}
Return sum;
}