給定數字0-9各若干個。你可以以任意順序排列這些數字,但必須全部使用。目標是使得最後得到的數儘可能小(注意0不能做首位)。例如:給定兩個0,兩個1,三個5,一個8,我們得到的最小的數就是10015558。
現給定數字,請編寫程序輸出能夠組成的最小的數。
輸入格式:
每個輸入包含1個測試用例。每個測試用例在一行中給出10個非負整數,順序表示我們擁有數字0、數字1、……數字9的個數。整數間用一個空格分隔。10個數字的總個數不超過50,且至少擁有1個非0的數字。
輸出格式:
在一行中輸出能夠組成的最小的數。
輸入樣例:2 2 0 0 0 3 0 0 1 0輸出樣例:
10015558代碼如下:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("D:\\in.txt", "r", stdin);
freopen("D:\\out.txt", "w", stdout);
#endif
int a[10];
while (scanf("%d%d%d%d%d%d%d%d%d%d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7], &a[8], &a[9]) != EOF)
{
vector<int> coll(a, a + 10);
string s = "";
int i;
for (i = 1; i < 10; i++)
{
if (coll[i] > 0)
break;
}
coll[i] = coll[i] - 1;
s += i + '0';
for (int j = 0; j < 10; j++)
{
for (int k = 0; k < coll[j]; k++)
s += j + '0';
}
cout << s << endl;
}
return 0;
}