PAT 1023. 組個最小數 (20)

給定數字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;

}

發佈了86 篇原創文章 · 獲贊 10 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章