题目链接:
CF 489 C Given Length and Sum of Digits…
题意:
给出m和s找到位数为m各位上数字和为s的最大数和最小数,不要前导0,如果找不到输出-1.
1<=m<=100,0<=s<=300
.
分析:
贪心思想。最大值一定是9尽量靠前,最小数一定是9尽量靠后,但是要保证首位至少为1.最大值不存在那就是每位上数字都为9仍然满足不了和为s,最小值不存在那就是s为0但是m>1.
注意:
s=0,m=1时需要特判。
//31MS 2000K
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
using namespace std;
const int maxn=110;
int m,s;
char s1[maxn],s2[maxn];
int main()
{
while(~scanf("%d%d",&m,&s)){
if(s>9*m||(s==0&&m>1)){
printf("-1 -1\n");
continue;
}
else if(s==0&&m==1){//Special
printf("0 0\n");
continue;
}
s1[m]=s2[m]='\0';
int tmp=s;
for(int i=0;i<m;i++){//MAX
if(tmp>=9){
s1[i]=9+'0';
tmp-=9;
}else if(tmp>0){
s1[i]=tmp+'0';
tmp=0;
}else{
s1[i]='0';
}
}
tmp=s-1;
for(int i=m-1;i>=0;i--){//MIN
if(tmp>9){
s2[i]=9+'0';
tmp-=9;
}else if(tmp>0){
s2[i]=tmp+'0';
tmp=0;
}else{
s2[i]='0';
}
}
s2[0]+=1;
printf("%s %s\n",s2,s1);
}
return 0;
}