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 ...
随机推荐
- PHP将Excel导入数据库以及数据库数据导出至Excel
一.导入 导入需要使用能读取Excel的组件,网上也有比较好的组件,这里分享我使用的:下载 提取码:vxyn.(注意两个文件有引用关系) <?php //传入要导入的Excel的文件名 fun ...
- IOS 学习笔记(1) 视图UIViewController
1.UIViewController *newController=[[UIViewController alloc] initWithNibName:@"XXX" bundle: ...
- 如何使用robots不让百度和google收录
如何使用robots不让百度和google收录 有没有想过,如果我们某个站点不让百度和google收录,那怎么办? 搜索引擎已经和我们达成一个约定,如果我们按约定那样做了,它们就不要收录. 这个写 ...
- Noip2010提高组总结
将Noip2010重新做了一遍,第一遍做下来居然只有290分,比当年浙江的一等线低了20分,因为各种坏习惯丢掉了许多分数,Noip时需要特别注意! T1:机器翻译 第一题直接暴力,内存足够所以不用循环 ...
- COBOL学习
COBOL概述 什么是COBOL语言: COBOL是Common Business Oriented Language的缩写,是面向商业通用编程语言.它是专门为商业数据处理而设计 ...
- HttpModule、HttpHandler和Page的生命周期
1.引言 2.两个处理步骤 2.1.创建ASP.NET环境 2.2.用触发的MHPM事件处理请求 3.什么事件中应该做什么 4.示例代码 5.深入ASP.NET页面事件 1.引言 这篇文章我们将试图理 ...
- TexturePacker 介绍
TexturePacker这个词从字面来说就是Texture(纹理) + Packer(打包). 它是一款把若干资源图片拼接为一张大图的工具.它由一位叫做Andreas Loew的老外开发的 :). ...
- 基于hash的文档判重——simhash
本文环境: python3.5 ubuntu 16.04 第三方库: jieba 文件寄于github: https://github.com/w392807287/angelo_tools.git ...
- debian下samba配置
debian下samba配置 http://blog.chinaunix.net/uid-2282111-id-2113216.html 服务器端配置过程:1. apt-get install sa ...
- JavaSE学习总结第09天_面向对象4
09.01 final关键字引入 例: class Fu { public final void show() { System.out.println("访问底层资源"); ...