zoukankan      html  css  js  c++  java
  • CF 489 C Given Length and Sum of Digits... 贪心

    题目链接http://codeforces.com/problemset/problem/489/C

    题目大意:给定位数和各个位的和,问满足条件数字的最大值,最小值。

    解题思路:模拟即可。主要是细节判断。

    代码:

     1 const int inf = 0x3f3f3f3f;
     2 const int maxn = 1e2 + 5;
     3 int m, s;
     4 char ans1[maxn], ans2[maxn];
     5 
     6 void solve(){
     7     memset(ans1, 0, sizeof(ans1));
     8     memset(ans2, 0, sizeof(ans1));
     9     
    10     if((s == 0 && m > 1) || s > m * 9) {
    11         printf("-1 -1
    ");
    12         return;
    13     }
    14     int x = s;
    15     for(int i = 1; i <= m; i++){
    16         int u = (m - i) * 9;
    17         if(u < x) {
    18             ans1[i] = (x - u + '0');
    19             x = u;
    20         }
    21         else if(u == x){
    22             if(i == 1) {
    23                 if(x > 0) {
    24                     ans1[i] = '1';
    25                     x -= 1;
    26                 }
    27                 else ans1[i] = '0';
    28             }
    29             else {
    30                 ans1[i] = '0';
    31             }
    32         }
    33         else if(u > x) {
    34             if(i == 1) {
    35                 ans1[i] = '1';
    36                 x -= 1;
    37             }
    38             else ans1[i] = '0';
    39         }
    40     }
    41     x = s;
    42     for(int i = 1; i <= m; i++){
    43         if(x >= 9) {
    44             ans2[i] = '9';
    45             x -= 9;
    46         }
    47         else{
    48             ans2[i] = (x + '0');
    49             x = 0;
    50         }
    51     }
    52     
    53     printf("%s %s
    ", ans1 + 1, ans2 + 1);
    54 }
    55 int main(){
    56     scanf("%d %d", &m, &s);
    57     solve();
    58 }

    题目:

    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
  • 相关阅读:
    day09 文件操作
    深信服二面
    test1
    视频测试
    通过独立按键控制LED灯
    第一个LED灯
    为什么我的递归调用次数和书上的不一样?
    函数指针数组
    虚拟内存
    单元测试
  • 原文地址:https://www.cnblogs.com/bolderic/p/7374656.html
Copyright © 2011-2022 走看看