CF489C Given Length and Sum of Digits...

原题链接

  • 题目:给了数字位数和数字位数之和,要构造出最大的数字和最小的数字。
  • 题解:就是想着让 (9) 往后并且让 (1) 往前,然后中间用 (0),注意无效的时候是 (s = 0) 并且 (m eq 1)
  • 代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 5e5 + 9;
const ll mod = 1e9 + 7;
bool vis[N];
char s[N];
int main() {
    string ans1, ans2;
    int m, s;
    cin >> m >> s;
    if ( s > 9 * m||s == 0) {
        if (m == 1&&s == 0)cout << 0 << " " << 0 << endl;
        else 
        cout <<"-1 -1
";
    }
    else {
        int S = s-1;
        for (int i = 1; i <= m; i ++) {
            int d = S;
            if (d >= 9) d = 9;
            S -= d;
            ans1 = (char)(d + '0') + ans1;
        }
        ans1[0]++;
        cout << ans1 << " ";
        ans1[0]--;
        reverse(ans1.bgin(), ans1.end());
        for (int i = 0; i < ans1.size(); i ++) {
            if (ans1[i] != '9') {
                ans1[i] ++;break;
            }
        }
        cout << ans1 << endl;
    }
}
原文地址:https://www.cnblogs.com/Xiao-yan/p/14734624.html