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\n");
            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\n");
            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("\n");
        for(i = 0; i < b.size(); ++i){
            printf("%c",b[i]);
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-09-28 13:03:14

Codeforces 489C Given Length and Sum of Digits...的相关文章

codeforces 489C.Given Length and Sum of Digits... 解题报告

题目链接:http://codeforces.com/problemset/problem/489/C 题目意思:给出 m 和 s,需要构造最大和最小的数.满足长度都为 m,每一位的数字之和等于 s.如果构造不出来,输出 -1 -1.否则输出最小和最大且符合条件的数. 想了两个多小时,发现想错了方向...... /****************************************** 首先把不能得到最小数和最大数的情况揪出来. 第二组测试数据 3 0 有提示,s = 0 且 m >

贪心/CodeForces 489C Given Length and Sum of Digits...

1 #include<cstdio> 2 using namespace std; 3 int m,s; 4 int main() 5 { 6 scanf("%d%d",&m,&s); 7 if (s==0 && m==1) printf("0 0\n"); 8 else if (s==0) printf("-1 -1\n"); 9 else if (s>m*9) printf("-1

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, sizeo

Codeforces Round #277.5 (Div. 2)C. Given Length and Sum of Digits...(贪心)

传送门 Description 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 d

Codeforces Round #277.5 (Div. 2)——C贪心—— Given Length and Sum of Digits

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 with

Codeforces Round #277.5 (Div. 2) C Given Length and Sum of Digits...

大大的传送门:就是这里 这一道卡在了特判的  1   0 本来输出 0  0 输出  -1    -1了,就错了,, 这道题就是一个恨好的贪心,往大了贪 下面是代码 #include <cstdio> #include <cstring> #include <vector> using namespace std; int n,m; int ans[110]; int main(){ scanf("%d%d",&n,&m); mems

C. Given Length and Sum of Digits...

http://codeforces.com/contest/489/problem/C 大数就是从最高位,能大就大:小数就是从最低位,能小就小,再处理下最高位为0的情况. 无结果无非一个sum太小,min全为0,一个sum太大,全为9还有剩 1 public class Main { 2 public static void main(String[] args) { 3 Scanner io = new Scanner(System.in); 4 int len = io.nextInt(),

8504. Sum of Digits

Sum of Digits Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a n

Sum of Digits is Prime

Sum of Digits is Prime Daoyi Peng August 19, 2014 For an integer $q\geqslant2$ let $s_q(n)$ denote the $q$-ary sum-of-digits function of a non-negative integer $n$, that is, if $n$ is given by its $q$-ary digits expansion $n=\sum\limits_{k=0}^{r} a_k