Codeforces Round #277.5 (Div. 2) C

C. Given Length and Sum of Digits...
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ms (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).

Sample test(s)
input
2 15
output
69 96
input
3 0
output
-1 -1
 
        
         
         
题意:给你m,s两个数,m代表位数,s代表这个数各个位上的数加起来的和,让我们求这样能组成的最大数和最小数,如果不存在则输出-1 -1。
做法:先判断是否存在这样的数,首先判断m*9>s或者(m>1&&s=0)是否成立,如果成立则不存在这样的数。最大值比较好找,从最前位开始每一位取min(s,9),取完之后s-取得值。最小值则要注意一下最前位,从最后一位开始,如果不是最前位则取min(s-1,9),如果是最前位则取min(s,9)。
#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include<ctime>
#define esp 1e-6
#define LL long long
#define inf 0x0f0f0f0f
using namespace std;
int main()
{
    int m,s,s1;
    int i,j,sum;
    int num[105],num2[105];
    while(scanf("%d%d",&m,&s)!=EOF)
    {
        s1=s;
        memset(num,0,sizeof(num));
        memset(num2,0,sizeof(num2));
        if(m==1&&s==0)
        {
            printf("0 0\n");
            continue;
        }
        if(m*9<s||(m>1&&s==0))
        {
            printf("-1 -1\n");
            continue;
        }
        if(m==1)
        {
            printf("%d %d\n",s,s);
            continue;
        }
        for(i=0;i<m;i++)
        {
            if(s>=9)
            {
                num[i]=9;
                s-=9;
            }
            else
            {
                num[i]=s;
                s-=num[i];
            }
        }
        for(i=m-1;i>=0;i--)
        {
            if(s1>=10&&i!=0)
            {
                num2[i]=9;
                s1-=9;
            }
            else if(s1>=9&&i==0)
                {
                     num2[i]=9;
                     s1-=9;
                }
            else if(s1>=2&&s1<=9&&i!=0)
            {
                num2[i]=s1-1;
                s1-=num2[i];
            }
            else if(s1>=1&&s1<=9&&i==0)
            {
                num2[i]=s1;
                s1-=num2[i];
            }
            else
                num2[i]=0;
        }
        for(i=0;i<m;i++)
            printf("%d",num2[i]);
        printf(" ");
        for(i=0;i<m;i++)
            printf("%d",num[i]);
        printf("\n");
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值