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 >= && * 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...的更多相关文章
- 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 ...
- 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 ...
- codeforces 489C.Given Length and Sum of Digits... 解题报告
题目链接:http://codeforces.com/problemset/problem/489/C 题目意思:给出 m 和 s,需要构造最大和最小的数.满足长度都为 m,每一位的数字之和等于 s. ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- CodeForces 489C (贪心) Given Length and Sum of Digits...
题意: 找出m位且各个数位数字之和为s的最大和最小整数,不包括前导0(比如说003是非法的),但0是可以的. 分析: 这题是用贪心来做的,同样是m位数,前面的数字越大这个数就越大. 所以写一个can( ...
- CF 277.5 C.Given Length and Sum of Digits.. 构造
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #incl ...
随机推荐
- ssh无密登录
ssh登录一般两种方式: 1.密码登录 2.密钥验证无需密码 使用方式:1.生成密钥 2.将公钥追加到authorized_keys中,需要注意的是执行权限需为600,这里因而第一次添加使用的是> ...
- phome_ecms_news 数据表字段解释(新闻系统模型-主表)
http://www.phome.net/doc/manual/extend/html/dbdoc/index.html 字段名 类型 解释 附加说明 id int(11) 信息ID classi ...
- HDU 1290 献给杭电五十周年校庆的礼物
题解:http://www.cnblogs.com/forever97/p/3522238.html #include <cstdio> int main() { int n; while ...
- 配置hibernate数据库连接
第一步:右键项目->MyEclipse->添加Hibernate组件,指定数据库连接配置如下(src/hibernate.cfg.xml) MySQL对连接的有效期是28800s,一个连接 ...
- Objective-c 类接口 (@interface) (类定义)
在Objective-c中如何定义一个类呢?我们可以使用下面的格式进行表示: @interface 类名:父类名{ 变量定义; } 方法定义: @end; 下面给出一个实例: @interface P ...
- D - D 田忌赛马
D - D 田忌赛马 解题报告 hdu 1052 Tian Ji -- The Horse Racing 链接:http://acm.hust.edu.cn/vjudge/contest/v ...
- C函数调用与栈--代码真相
前面详细的说了,C函数调用的过程中,栈的变化情况的原理部分,这里在看一下汇编代码的真正的实现. 有关前面的那一片博客,主要记住的就是函数调用时栈的变化,4+3+2的步骤: (1)设置栈帧边界 (2)开 ...
- Resource is out of sync with the file system: 解决办法
在eclipse中,启动server时报此错,是因为文件系统不同步造成的,解决方法有两个: (1)选中工程,右键,选择F5(手动刷新): (2)Window->Preferences->G ...
- isdigit()判断是不是数字
string 里面的函数isdigit(),可以判断是不是数字. 或者,采用type(1)==int.
- A. Liserious战队
Liserious战队是一支活跃在京沪地区的著名密室逃脱战队,在优秀的队长Liserious和芳姐的带领下,打遍天下大大小小的密室逃脱,攻无不克,战无不胜,上天入地无所不能. 世界密室逃脱委员会以IS ...