1023. Minimum number of groups (20)
Several numbers are given from 0 to 9. You can arrange these numbers in any order, but you must use them all. The goal is to make the resulting number as small as possible (note that 0 cannot be the first). For example: given two 0s, two 1s, three 5s, and an 8, the smallest number we can get is 10015558.
Given a number, write a program to output the smallest number that can be formed.
Input format:
Each input contains 1 test case. Each test case gives 10 non-negative integers in a row, in the order that we have the number 0, the number 1, ... the number 9. Separate integers with a space. The total number of 10 numbers does not exceed 50, and there is at least one non-zero number.
Output format:
Print the smallest number that can be formed on a line.
Input sample:2 2 0 0 0 3 0 0 1 0Sample output:
10015558
#include<bits/stdc++.h> using namespace std; int main(){ int num[10]; bool flag = false; for(int i = 0; i < 10; i++){ cin>>num[i]; } if(num[0] >= 0){ for(int i = 1; i < 10; i++){ if(num[i] >= 1){ cout<<i; num[i]--; flag = true; } if(flag){ for(int j = 0; j < num[0];j++){ cout<<"0"; } flag = false; break; } } } for(int i = 1; i < 10; i++){ for(int j = 0; j < num[i]; j++){ cout<<i; } } return 0; }