首页 > 其他 > 详细

CF489C Given Length and Sum of Digits...

时间:2021-05-06 14:44:52      阅读:30      评论:0      收藏:0      [点我收藏+]

原题链接

  • 题目:给了数字位数和数字位数之和,要构造出最大的数字和最小的数字。
  • 题解:就是想着让 \(9\) 往后并且让 \(1\) 往前,然后中间用 \(0\),注意无效的时候是 \(s = 0\) 并且 \(m \neq 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\n";
    }
    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;
    }
}

CF489C Given Length and Sum of Digits...

原文:https://www.cnblogs.com/Xiao-yan/p/14734624.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!