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 >= && * 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 = ; i < m; ++i){
for(d = ; d < ; ++d){
if((i > || d > || == m && == d) && judge(m - i - , sum - d)){ //handle preamble 0
a.push_back('' + 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 = ; i < m; ++i){
for(d = ; d >= ; --d){
if(judge(m - i - , sum - d)){
b.push_back('' + d);
sum -= d;
break;
}
}
} for(i = ; i < a.size(); ++i){
printf("%c",a[i]);
}
printf("\n");
for(i = ; i < b.size(); ++i){
printf("%c",b[i]);
}
printf("\n");
}
return ;
}

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

  1. CodeForces 489C Given Length and Sum of Digits... (贪心)

    Given Length and Sum of Digits... 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/F Descr ...

  2. CodeForces 489C Given Length and Sum of Digits... (dfs)

    C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...

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

    题目链接:http://codeforces.com/problemset/problem/489/C 题目意思:给出 m 和 s,需要构造最大和最小的数.满足长度都为 m,每一位的数字之和等于 s. ...

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

    http://codeforces.com/problemset/problem/489/C C. Given Length and Sum of Digits... time limit per t ...

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

    C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...

  6. codeforces#277.5 C. Given Length and Sum of Digits

    C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...

  7. B - Given Length and Sum of Digits... CodeForces - 489C (贪心)

    You have a positive integer m and a non-negative integer s. Your task is to find the smallest and th ...

  8. CodeForces 489C (贪心) Given Length and Sum of Digits...

    题意: 找出m位且各个数位数字之和为s的最大和最小整数,不包括前导0(比如说003是非法的),但0是可以的. 分析: 这题是用贪心来做的,同样是m位数,前面的数字越大这个数就越大. 所以写一个can( ...

  9. CF 277.5 C.Given Length and Sum of Digits.. 构造

    #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #incl ...

随机推荐

  1. KVO 的使用和举例

    KVO(key-value Observer),通过命名可以联想到,一个监视着监视着键值配对,让一个对象A来监视另一个对象B中的键值,一旦B中的受监视键所对应的值发生了变化,对象A会进入一个回调函数, ...

  2. android LayoutInflater的使用

    看其继承关系: public abstract class LayoutInflater extends Object java.lang.Object ↳ android.view.LayoutIn ...

  3. The request failed with HTTP status 401: Unauthorized.

    Reporting Service 控件默认由IIS里面的应用程序池标识 里面所定义的用户连接,如果用户没有权限则报以下错误 The request failed with HTTP status 4 ...

  4. C# 读书笔记之继承与多态

    1.1继承与多态的基本概念 1.1.1 继承和多态 继承是面向对象程序设计的主要特征之一,允许重用现有类(基类,亦称超类.父类)去创建新类(子类,亦称派生类)的过程.子类将获取基类的所有非私有数据和行 ...

  5. poj 1386 Play on Words(有向图欧拉路+并查集)

    题目链接:http://poj.org/problem?id=1386 思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指 ...

  6. Book of Evil 树双向DFS

    Book of Evil Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area ...

  7. SharePoint 2010 BCS - 简单实例(一)数据源加入

    博客地址 http://blog.csdn.net/foxdave 本篇基于SharePoint 2010 Foundation. 我的数据库中有一个病人信息表Patient,如今我就想把这个表中的数 ...

  8. 3.3 用NPOI操作EXCEL--生成一张工资单

    这一节,我们将综合NPOI的常用功能(包括创建和填充单元格.合并单元格.设置单元格样式和利用公式),做一个工资单的实例.先看创建标题行的代码: //写标题文本 HSSFSheet sheet1 = h ...

  9. 初始Android-配置环境

    最近闲来无事自学了一下Android,今天没事想整理一下思绪,简单的介绍一下我自己对环境配置的认识,仅供参考,欢迎提出意见. 1.首先打开Eclipse,然后安装ADT,准备好ADTjar包或者zip ...

  10. CocoaPods对于不同Target引入不同的第三方库Podfile的写法

    有的时候我们需要建立多个Target来完成不同的测试环境的区分,而多个Target之间可能会有第三方库的不同引用,如果我们在使用CocoaPods管理我们的第三方库的时候,我们就需要思考我们需要如何实 ...