PAT-B 1023. Min number of groups (20), pat-b1023
1023. Minimum number of groups (20)
The number 0-9 is given. You can arrange these numbers in any order, but they must all be used. The goal is to make the final number as small as possible (note that 0 cannot be the first place ). For example, given two 0 s, two 1 s, three 5 S, and one 8 s, the minimum number we get is 10015558.
Given a number, write a program to output the smallest number.
Input Format:
Each input contains one test case. Each test case contains 10 non-negative integers in one row, indicating that we have numbers 0, numbers 1 ,...... The number of digits 9. An integer is separated by a space. The total number of 10 numbers cannot exceed 50, and at least one number is not 0.
Output Format:
Output the smallest number in a row.
Input example:
2 2 0 0 0 3 0 0 1 0
Output example:
10015558
Train of Thought Analysis: Put the 10 numbers in an array first, take out the smallest number (except 0), and mark it. And then output in ascending order.
1 #include <cstdio> 2 int main() { 3 int a[10]; 4 int i; 5 for( i = 0; i < 10; i++ ) { 6 scanf( "%d", &a[i] ); 7 } 8 9 int min;10 for( i = 0; i < 10; i++ ) {11 if( i != 0 && a[i] != 0 ) {12 min = i;13 break;14 }15 }16 printf( "%d", min );17 a[min]--;18 for( i = 0; i < 10; i++ ) {19 for( ; a[i] > 0; a[i]-- ) {20 printf( "%d", i );21 }22 }23 return 0;24 }