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

    m位长度,S为各位的和

    利用贪心的思想逐位判断过去即可

    详细的注释已经在代码里啦~

    //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
    #include <stdio.h>
    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <algorithm>
    #define ll long long
    #define Max(a,b) (((a) > (b)) ? (a) : (b))
    #define Min(a,b) (((a) < (b)) ? (a) : (b))
    #define Abs(x) (((x) > 0) ? (x) : (-(x)))
    
    const int INF = 0x3f3f3f3f;
    
    vector <char> a, b;
    
    bool judge(int m, int s){   //judge whether m long s sum valid
        return s >= 0 && 9 * m >= s;
    }
    
    int main(){
        int i, j, d, m, s;
        while(EOF != scanf("%d%d",&m,&s)){
            if(!judge(m, s)){
                printf("-1 -1
    ");
                continue;
            }
            a.clear();
            b.clear();
    
            int sum = s;
            for(i = 0; i < m; ++i){
                for(d = 0; d < 10; ++d){
                    if((i > 0 || d > 0 || 1 == m && 0 == d) && judge(m - i - 1, sum - d)){ //handle preamble 0 
                        a.push_back('0' + d);
                        sum -= d;
                        break;
                    }
                }
            }
    
            if(a.size() != m){  // if exist an answer, it proves that both existing a, b
                printf("-1 -1
    ");
                continue;
            }
    
            sum = s;
            for(i = 0; i < m; ++i){
                for(d = 9; d >= 0; --d){
                    if(judge(m - i - 1, sum - d)){
                        b.push_back('0' + d);
                        sum -= d;
                        break;
                    }
                }
            }
    
            for(i = 0; i < a.size(); ++i){
                printf("%c",a[i]);
            }
            printf("
    ");
            for(i = 0; i < b.size(); ++i){
                printf("%c",b[i]);
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    栈的应用之银行叫号系统模拟
    栈的应用之括号匹配
    栈的应用之数制转换
    线性结构 一元多项式的乘法与加法运算
    Checkpoints codeforces 709B
    寒冰王座 hdu 1248(背包)
    单链表头插法、尾插法(26个字母为例)
    两个有序单链表的合并
    Number Sequence HDU 1711(KMP)
    完成运算
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/4123576.html
Copyright © 2011-2022 走看看