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. 指针和引用区别 C++

    #include <iostream> using namespace std; int main(){ ; void cubeByPoint(int *);//指针传参声明 void c ...

  2. 把一个数组向右循环移动k位要求时间复杂度为O(n)

    今晚做了下某公司的网络笔试题,好久没刷题了,现在渣得要死,里面有道程序设计题是 把一个数组向右循环移动k位要求时间复杂度为O(n) 给的方法定义为 public void solution(int a ...

  3. Home | WebScraping.com

    Home | WebScraping.com We specialize in extracting data from websites, which is known as web scrapin ...

  4. linux64位系统中g++4.4.7下使用wcsncpy函数有造成段错误的BUG(其它g++版本号未知)

    一.描写叙述 眼下已经linux64位系统中g++4.4.7下发现了该bug.其它的版本号还未測试. 经測试,假设传入wcsncpy函数第一个參数(即目标缓冲区)的地址不能被4整除的话就必然会发生段错 ...

  5. 开发板-PC机(宿主机)-虚拟机(VM)之间网络通信设置方法及须要注意的问题

    1.不使用路由器交换机 硬件连接: 使用网线将开发板和PC机相连 串口线将PC机和开发板相连 使用命令: ifconfig -a 串口控制端查看开发板的网络配置 route -n 串口控制端查看开发板 ...

  6. WPF与Winform的选择

    最近公司计划对ERP系统全面升级,现有的ERP是简单的bs架构系统打算改版成cs.平时如自己写一些工具,小应用都是用winform就足够.但是界面总是很难看,据了解WPF在这一方面会强一些.因为之前对 ...

  7. json数据的获取(网络摘抄)

    一个简单的对象: $.ajax({ type: "post", url: "Handler.ashx", dataType: "html", ...

  8. 初学MVC

    学习MVC基础:C#. ADO.NET .html.javascript.ASP.Net .WebFrom MVC模式两种理解:一种是表现模式,另一种是架构模式.它将应用程序分成三个主要的组件:视图( ...

  9. 第一篇:GCD多线程的概念

    1.什么叫GCD? 简单来说就是:Grand Central Dispatch的简称,中文翻译就是:”牛逼的中枢调度器“ 这是纯C语言,还提供了非常多强大的函数 2.GCD的相对优势: (1)GCD是 ...

  10. ansible笔记

    ansible 资料 ansible 配置 ansible inventory配置文件 ansible模块 http://www.cnblogs.com/iois/p/6216936.html ans ...