zoukankan      html  css  js  c++  java
  • 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<iostream>
    #include<cstdlib>
    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;
    }
  • 相关阅读:
    KMP算法小结
    算法二叉搜索树之AVL树
    算法导论之红黑树的学习
    算法导论小结(一)
    感悟或摘抄
    js中神奇的东西
    简单了解webservice
    用<![CDATA[]]>将xml转义为 纯文本
    简单了解soap协议
    java写webservice接口
  • 原文地址:https://www.cnblogs.com/chenruijiang/p/7890940.html
Copyright © 2011-2022 走看看