题目链接: Given Length and Sum of Digits…
大致题意:
给出长度m,和s,计算十进制下每一位总和是s,长度是m的最小值和最大值
解题思路:
构造+贪心
注意:不能有前导零
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int m, s; cin >> m >> s;
if (s < 1 && m>1 || s > m * 9)
cout << -1 << ' ' << -1 << endl;
else {
for (int i = m - 1, k = s; i >= 0; --i) {
int x = max(0, k - 9 * i);
if (!x && i == m - 1 && k)x = 1;
cout << x; k -= x;
}
cout << ' ';
for (int i = m - 1, k = s; i >= 0; --i) {
int x = min(9, k);
cout << x;
k -= x;
}
}
return 0;
}