489C - Given Length and Sum of Digits...

链接:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值