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.
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.
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).
2 15
69 96
3 0
-1 -1
题意:
给两个数m, s,求出位数等于m 且个位数字之和等于s的最小数和最大数。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int m,s,a[105],b[105];
string minx = "", maxx = "";
int main()
{
scanf("%d%d",&m, &s);
if(m == 1&&s == 0) printf("0 0\n");
else if(m*9 < s||s == 0) printf("-1 -1\n");
else {
int ss = s - 1;
for(int i = 0; i < m; i++) a[i] = min(s, 9), s -= a[i]; //求最大值
for(int i = 0; i < m; i++) maxx += (a[i] + '0');
memset(b, 0, sizeof(b)); //求最小值
b[0] = 1;
for(int i = m - 1; i > 0; i--) b[i] = min(ss, 9), ss -= b[i];
if(ss) b[0] += ss;
for(int i = 0; i < m; i++) minx += (b[i] + '0');
cout<<minx<<" "<<maxx<<endl;
return 0;
}
}