PAT (Basic Level) Practice (Chinese) 1023 set a minimum number (20 points) (sort)

0-9 each given a number. You can arrange these numbers in any order, but must be fully utilized. The goal is to make the resulting number as small as possible (do not pay attention to the first 0). For example: Given two 0, 1 two, three 5, a 8, we get the minimum number is 10015558.

The minimum number is now given a number, please write program output can be composed of.

Input formats:

Input 10 is given a non-negative integer in a row, we have the sequence represents the number 0, the number of number 1, number 9 ....... Separated by a space between integer. The total number of 10 digits is no more than 50, and has at least one non-zero digit.

Output formats:

The smallest number in a row can be output thereof.

Sample input:

2 2 0 0 0 3 0 0 1 0

Sample output:

10015558

In essence, or sorting, only the first position is not zero, if the results of the first sort is 0, and it will be the first non-zero number can be interchanged
 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 int a[12];
 5 int b[100];
 6 int main()
 7 {
 8     int t=0;
 9     for(int i=0;i<10;i++){
10         cin>>a[i];
11         for(int j=t;j<=t+a[i];j++){
12             b[j]=i;
13         }
14         t=t+a[i];
15     }
16     sort(b,b+t);
17     if(b[0]==0){
18         for(int i=1;i<t;i++){
19             if(b[i]>0){
20                 int t=b[i];
21                 b[i]=b[0];
22                 b[0]=t;
23                 break;
24             }
25         }
26     }
27     for(int i=0;i<t;i++){
28         cout<<b[i];
29     }
30     cout<<endl;
31       return 0;
32 }

 

Guess you like

Origin www.cnblogs.com/shixinzei/p/11069454.html