codeforces-489C

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

Examples
input
2 15
output
69 96
input
3 0
output
-1 -1
这道题目的要求大致是输入位数m和位数和s,输出最小数和最大数,没有就输出两个-1。
基本思路是高位先用9,然后低位补齐,最小数倒过来最高位用1,低位用9。
#include
#include
using namespace std;
int main()
{
    int m, s, i, j;
    cin >> m >> s;
    if (m == 1 && s < 10)
    {
        cout << s << ' ' << s << endl;
        return 0;
    }
    else if (s == 0||(m * 9 < s))
    {
        cout << -1 << ' ' << -1 << endl;
        return 0;
    }
    int a[105] = {0};
    int sum = s;
    for (i = 0;i < m;i++)
    {    
        if (sum <= 9)
        {
            a[i] = sum;
            j = i;
            break;
        }
        a[i] = 9;
        sum -= 9;        
    }
    if (a[m - 1] != 0)
        for (i = m - 1;i >= 0;i--)
            cout << a[i];
    else
    {
        cout << 1;
        for (i = m - 2;i >= 0;i--)
        {
            if (i == j)
            {
                cout << a[i] - 1;
                continue;
            }
            cout << a[i];
        }
    }
    cout << ' ';
    for (i = 0;i < m;i++)
        cout << a[i];
    cout << endl;
    return 0;
}

 

转载于:https://www.cnblogs.com/chenruijiang/p/7890940.html

你可能感兴趣的:(codeforces-489C)