Codeforces Round #277.5 (Div. 2) C

C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base without leading zeroes.
Input 【Codeforces Round #277.5 (Div. 2) C】 The single line of the input contains a pair of integers m, s (1?≤?m?≤?100,?0?≤?s?≤?900) — the length and the sum of the digits of the required numbers.
Output In the output print the pair of the required non-negative integer numbers — first the minimum possible number, then — the maximum possible number. If no numbers satisfying conditions required exist, print the pair of numbers "-1 -1" (without the quotes).
Sample test(s) input

2 15

output
69 96

input
3 0

output
-1 -1



题意:给你m,s两个数,m代表位数,s代表这个数各个位上的数加起来的和,让我们求这样能组成的最大数和最小数,如果不存在则输出-1 -1。 做法:先判断是否存在这样的数,首先判断m*9>s或者(m>1&&s=0)是否成立,如果成立则不存在这样的数。最大值比较好找,从最前位开始每一位取min(s,9),取完之后s-取得值。最小值则要注意一下最前位,从最后一位开始,如果不是最前位则取min(s-1,9),如果是最前位则取min(s,9)。
#include #include #include #include #include #include #include #include #include #include #include #include #define esp 1e-6 #define LL long long #define inf 0x0f0f0f0f using namespace std; int main() { int m,s,s1; int i,j,sum; int num[105],num2[105]; while(scanf("%d%d",&m,&s)!=EOF) { s1=s; memset(num,0,sizeof(num)); memset(num2,0,sizeof(num2)); if(m==1&&s==0) { printf("0 0\n"); continue; } if(m*91&&s==0)) { printf("-1 -1\n"); continue; } if(m==1) { printf("%d %d\n",s,s); continue; } for(i=0; i=9) { num[i]=9; s-=9; } else { num[i]=s; s-=num[i]; } } for(i=m-1; i>=0; i--) { if(s1>=10&&i!=0) { num2[i]=9; s1-=9; } else if(s1>=9&&i==0) { num2[i]=9; s1-=9; } else if(s1>=2&&s1<=9&&i!=0) { num2[i]=s1-1; s1-=num2[i]; } else if(s1>=1&&s1<=9&&i==0) { num2[i]=s1; s1-=num2[i]; } else num2[i]=0; } for(i=0; i



    推荐阅读