组个最小数

题目描述

给定数字0-9各若干个。你可以以任意顺序排列这些数字,但必须全部使用。目标是使得最后得到的数尽可能小(注意0不能做首位)。例如:
给定两个0,两个1,三个5,一个8,我们得到的最小的数就是10015558。

现给定数字,请编写程序输出能够组成的最小的数。

#include <iostream>

using namespace std;

int main()
{
    int a[10] = { 0 };
    int i, j = 0, num = 0, k;
    cout << "Please enter the numbers:";
    cin >> a[j];
    j++;
    while (j < 10) {
        cin >> a[j];
        j++;
    }

    if(a[0] != 0) {
        num++;
        while (a[num] == 0) {
            num++;
        }
        cout << num;
    }

    a[num] = a[num] - 1;
    
    for (num = 0; num<10;num++) {
        if (a[num] != 0) {
            for (k = 0; k < a[num]; k++)
                   cout << num;        
        }    
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42082542/article/details/82985631