PAT乙级1023

题目链接

https://pintia.cn/problem-sets/994805260223102976/problems/994805298269634560

题解

主要就是控制首位不能为0,其他的都很简单,就遍历然后往尾部加数字就好了。

// PAT BasicLevel 1023
// https://pintia.cn/problem-sets/994805260223102976/problems/994805298269634560

#include <iostream>
using namespace std;

int main()
{
    // 结果
    string res="";
    
    // 获取数字零的数量
    int zeroCount;
    cin >>zeroCount;

    // 标志是否为第一个数量超过0个的数字
    bool isFirst=true;

    // 非零数字个数
    int count;

    // 获取非零数字的数量并生成最终结果
    for(char c='1';c<='9';++c){
        // 获取非零数数量
        cin >> count;
        
        // 该数字的数量超过0个时
        if(count>0){
            
            // 如果是第一个数量超过0的非零数字
            if (isFirst){
                // 先把它加在最前边,因为首位不可以是0
                res+=c;
                count--;

                // 把零加在前边
                for(int i=0;i<zeroCount;++i){
                    res+='0';
                }

                // 更新标志
                isFirst=false;
            }
            // 加自己
            for (int i = 0; i < count; ++i){
                res += c;
            }
        }
    }

    // 输出形成的最小数
    cout << res;

    //system("pause");
    return 0;
}

作者:@臭咸鱼

转载请注明出处:https://www.cnblogs.com/chouxianyu/

欢迎讨论和交流!


原文地址:https://www.cnblogs.com/chouxianyu/p/11309268.html