CodeForces 489C
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
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).
您有一个正整数m和一个非负整数s。您的任务是找到长度为m且数字总和为s
的最小和最大数字。所需的数字应为以小数进制书写的非负整数,不带前导零。 输入 输入的单行包含一对整数m、s(1 ≤ m ≤ 100, 0 ≤
s ≤ 900)— 所需数字的长度和数字之和。 输出 在输出中,打印所需的非负整数对 -
首先是最小可能数,然后是最大可能数。如果没有满足条件的数字存在,请打印一对数字"-1 -1"(不带引号)。
Examples
Input
2 15
Output
69 96
Input
3 0
Output
-1 -1
解题思路:
求最大可能数时,让前面的数字尽可能大,所以优先考虑9,当剩下的数小于9时,加上,如果为数不够,用0补充位数。
求最小可能数时,将最大可能数倒序输出。如果最大可能数的后面是0时不能直接倒序输出(不带前导0),我们可以将第一个不为0的数减1,将1放在最小可能数的最前面。
特判条件:
当m*9<s 和 m>1&&s=0时,结果不存在。直接输出(“-1 -1”);还有就是当m=1&&s=0时,答案为(“0 0”);
#include<iostream>
using namespace std;
int main() {
int m,s;
cin>>m>>s;
if(m*9<s||(m>1&&s==0)) {
printf("-1 -1");
return 0;
}
if(m==1&&s==0) {
printf("0 0");
return 0;
}
int Max[200];
int count2=0;
for(int i=0; i<m; i++) {
if(s>=9) {
Max[i]=9;
s=s-9;
} else if(s!=0) {
Max[i]=s;
s=0;
} else if(s==0) {
Max[i]=0;
count2++;
}
}
//Min
int flag=1;
int flag1=1;
for(int i=m-1; i>=0; i--) {
if(Max[m-1]==0&&flag==1) {
printf("1");
flag=0;
continue;
}
if(Max[m-1]==0&&Max[i]!=0&&flag1==1) {
printf("%d",Max[i]-1);
flag1=0;
continue;
}
printf("%d",Max[i]);
}
//
printf(" ");
//Max
for(int i=0; i<m; i++) {
printf("%d",Max[i]);
}
}