PAT乙级1023

题目链接

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

题解

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 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/

欢迎讨论和交流!