题目
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).
Examples
Input
2 15
Output
69 96
Input
3 0
Output
-1 -1
大意:输入 m和s两个数。在m位的所有数字中,找到加起来的和是s的最大最小两个数
思路
一.特殊情况直接输出
if(m==1&&s==0)cout<<"0"<<" "<<"0"<<endl;
else if(m*9<s||s==0)cout<<"-1"<<" "<<"-1"<<endl;
二.从右往左填充数字。
如果 9< s,那么把9填进去,再从s中减去9,然后填下一位
如果 9>s,则说明不能进位了 那么把s填入
示例:
m=5 s=21
9<21 存9 s=12
9<12 存9 s=3
9>3 存3 s=0
从右向左存 那么存入的数就是399
三.再进行下一步的操作
最大数:
把已经存的数倒着输出,不够位数的话,直接用0补齐
此时存了3位数 不够5位
那么倒过来输出后其余位补零
则最大数是99300
最小数:
如果够位数,直接输出
如果不够位数,最小数的第一位是1,刚才存的数最左边的数减1
此时存了3位数 不够5位
那么最小数的第一位是1 则变为10399
存的数最左边的数减1 则变为10299
则 最小数是10299
代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int m,s,a[104]={0},b[104]={0};
cin>>m>>s;
if(m==1&&s==0)cout<<"0"<<" "<<"0"<<endl;
else if(m*9<s||s==0)cout<<"-1"<<" "<<"-1"<<endl;
else
{
a[1]=1;
int i,t;
for(i=m,t=1;i>=1&&s!=0;i--,t++)
{
a[i]=min(9,s);
b[t]=a[i];
s=s-a[i];
}
if(i!=0){a[i+1]-=1;}
for(int k=1;k<=m;k++)
{
cout<<a[k];
}
cout<<" ";
for(int k=1;k<=m;k++)
{
cout<<b[k];
}
}
return 0;
}