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;
    }
  • 相关阅读:
    对我影响最大的老师
    介绍自己
    JavaScript 时间特效 显示当前时间
    js 获取函数的所有参数名
    node.js 在函数内获取当前函数
    js 实现二叉排序树
    命令行下mysql的部分操作
    浅析js的函数的按值传递参数
    返回上一页时,保存恢复浏览记录(模拟返回不刷新)
    让mongodb执行js文件
  • 原文地址:https://www.cnblogs.com/chenruijiang/p/7890940.html
Copyright © 2011-2022 走看看