Codeforces Round #277.5 (Div. 2) C

来源:互联网 发布:拳师7号的淘宝店地址 编辑:程序博客网 时间:2024/06/05 05:03
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 0x0f0f0f0fusing 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
原创粉丝点击