Day 24 C. Given Length and Sum of Digits...

该程序解决了一道算法问题,输入包含数字的位数m和各位数字之和s,输出符合条件的最大和最小非负整数。如果不存在满足条件的数字,则输出-1-1。程序首先检查特殊情况,然后通过填充9来找到最小值,并从高位开始填充9以获得最大值。
摘要由CSDN通过智能技术生成

Problem
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

题目大致意思为:给定数字的位数和各个位数之和 求符合条件下的最大数值和最小数值

#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#define ll long long
int a[105], b[105];
using namespace std;
int main()
{
	int m, s;//m是位数 s是各位数之和
	cin >> m >> s;
	if (m == 1 && s == 0)
	{
		cout << 0 << " " << 0;
	}
	//如果各位数都为9的情况下仍小于题目给出的各位数之和 
	//或者在位数不为1的情况下各位数之和为0 
	//那么不符合题意 输出-1 -1
	else if (m * 9 < s || s < 1)
	{
		cout << -1 << " " << -1;
	}
	else
	{
		int x = s;
		//从最低位开始找 为了让数字最小 低位都填满9
		for (int i = m; i >= 1; i--)
		{
			a[i] = min(x, 9);
			x = x - min(x, 9);
		}
		if (a[1] == 0)
		{
			for (int i = 1; i <= m; i++)
			{
				if (a[i] != 0)
				{
					a[i] = a[i] - 1;
					a[1] = 1;
					break;
				}
			}
		}
		//从最高位开始找 为了让数字最大 高位都填满9
		x = s;
		for (int i = 1; i <= m; i++)
		{
			b[i] = min(x, 9);
			x = x - min(x, 9);
		}
		for (int i = 1; i <= m; i++)
		{
			cout << a[i];
		}
		cout << " ";
		for (int i = 1; i <= m; i++)
		{
			cout << b[i];
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值