链接:
https://codeforces.com/problemset/problem/489/C
题意:
给定长度m和十进制各位数字之和s,求满足m,s的最大值和最小值,没有输出-1 -1;
输入
2 15
输出量
69 96
输入
3 0
输出量
-1 -1
解:
先特判-1 -1情况(m为数字各位数之和最大m*9。s=0,但是m>1时,因为无法存在前缀0,所以不成立)
其他情况下,最小数就是第一位从1开始,其他位置从0开始,要满足后面的位数足够满足s;
最大声从9开始满足s;
查看WA发现m=1,s=0答案是0 0,加了特判
实际代码:
#include<iostream>
using namespace std;
int main()
{
int ys;
int m,s;
cin>>m>>s;
ys=s;
if(s < 1 && m>1||s>m*9) cout<<"-1 -1"<<endl;
else if(m==1&&s==0) cout<<"0 0"<<endl;
else
{
for(int i=1;i<=m;i++)
{
//cout<<"i"<<i<<endl;
if(i==1)
{
for(int j=1;j<=9;j++)
{
if(s-j<=((m-i)*9))
{
cout<<j;
s-=j;
//cout<<j<<"yes"<<endl;
break;
}
}
}
else
{
for(int j=0;j<=9;j++)
{
if(s-j<=((m-i)*9))
{
cout<<j;
s-=j;
//cout<<j<<"yes"<<endl;
break;
}
}
}
}
cout<<" ";
s=ys;
for(int i=1;i<=m;i++)
{
for(int j=9;j>=0;j--)
{
if(s-j>=0)
{
s-=j;
cout<<j;
break;
}
}
}
}
}
限制:
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output