CodeForces 489C Given Length and Sum of Digits...

题意:给你字符串的长度和总和,让你构造出最大的数和最小的数,如果不行输出-1

思路:大佬们的思路还真是强啊,最大的数很容易做,最小的数是把最大的数进行反转,然后找到一个不为0的地方--,在前面++;

代码:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int m,s;
    while(~scanf("%d%d",&m,&s)){
        if(m==1&&s==0){
            printf("0 0
");
            continue;
        }
        if(s==0||9*m<s){
            printf("-1 -1
");
            continue;
        }
        string s1,s2;
        for(int i=0;i<m;i++){
            int x=min(9,s);
            s-=x;
            s2+=char(x+'0');
        }
        s1=s2;
        reverse(s1.begin(),s1.end());
        bool ok=true;
        for(int i=0;i<m&&ok;i++){
            if(s1[i]=='0'){
                for(int j=i+1;j<m&&ok;j++){
                    if(s1[j]!='0'){
                        s1[j]--;
                        s1[i]++;
                        ok=false;
                        break;
                    }
                }
            }
        }
        cout<<s1<<" "<<s2<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lalalatianlalu/p/8452024.html