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 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 without leading zeroes.
Input
The single line of the input contains a pair of integers m, s (1 ≤ m ≤ 100, 0 ≤ s ≤ 900) — the length and the sum of the digits of the required numbers.
Output
In the output print the pair of the required non-negative integer numbers — first the minimum possible number, then — the maximum possible number. If no numbers satisfying conditions required exist, print the pair of numbers "-1 -1" (without the quotes).
Examples
2 15
69 96
3 0
-1 -1
题意是:给出m,s。求出长度为m的数,各个位上的数加起来为s。求最小的和最大的数。
贪心的思想,对于最小的,从最右边开始,拿最大的往上怼,不能有前导零(而求最大时可以后面有零,这是区别)
对于最大的,从最左边开始,拿最大的往上怼,不用在乎后面为0,因为这样才能为最大。
对与输出为-1 -1 的情况,如果m*9<s,或者:s=0 时,只有m=1时有答案0,0。m>1时为-1 -1;
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
ll a[],b[];
ll tot=;
int main()
{
ll m,s;
while(cin>>m>>s)
{
if(m*<s||(s<&&m!=))
cout<<"-1 -1"<<endl;
else
{
int m1=m,s1=s;
for(int i=m-;i>=;i--)
{
if(s>)
{
a[i]=;
s-=;
} //还没到最后一位,尽量大得放
else if(i!=)
{
a[i]=s-;
s=; //这个是为了前一位尽量为1,因为此时已经小于9了;如果还没到第一位,而s=1,那么就上0;
}
else
{
a[i]=s;//到第一位了,那就没得分了,直接往上放。
}
} //如果大于9,肯定往上放9。
for(int i=;i<m1;i++)
{
if(s1>)
{
b[i]=;
s1-=;
}
else
{
b[i]=s1;
s1=;
} }
for(int i=;i<m;i++)
printf("%d",a[i]);
printf(" ");
for(int i=;i<m;i++)
printf("%d",b[i]);
cout<<endl;
}
}
}
B - Given Length and Sum of Digits... CodeForces - 489C (贪心)的更多相关文章
- 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 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 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 ...
- 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 489C (贪心) Given Length and Sum of Digits...
题意: 找出m位且各个数位数字之和为s的最大和最小整数,不包括前导0(比如说003是非法的),但0是可以的. 分析: 这题是用贪心来做的,同样是m位数,前面的数字越大这个数就越大. 所以写一个can( ...
- Codeforces 489C Given Length and Sum of Digits...
m位长度,S为各位的和 利用贪心的思想逐位判断过去即可 详细的注释已经在代码里啦~ //#pragma comment(linker, "/STACK:16777216") //f ...
- CF 277.5 C.Given Length and Sum of Digits.. 构造
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #incl ...
随机推荐
- Vuex源码分析(转)
当我们用vue在开发的过程中,经常会遇到以下问题 多个vue组件共享状态 Vue组件间的通讯 在项目不复杂的时候,我们会利用全局事件bus的方式解决,但随着复杂度的提升,用这种方式将会使得代码难以维护 ...
- 吴裕雄--天生自然java开发常用类库学习笔记:比较器
class Student implements Comparable<Student> { // 指定类型为Student private String name ; private i ...
- Linux netfliter 架构
netfliter 简介 netfilter是在Linux 2.4.X内核引入的一个子系统,它提供了一个抽象的.通用框架,这个框架提供了一整套的钩子函数的管理机制.包括钩子函数的原型定义,注册,注销等 ...
- P1085 PAT单位排行
转跳点:
- 【pwnable.kr】cmd2
这道题是上一个cmd1的升级版 ssh cmd2@pwnable.kr -p2222 (pw:mommy now I get what PATH environmentis for :)) 登录之后, ...
- 云时代架构阅读笔记一——Java性能优化(一)
Java语言学习了这么长时间之后,自己对于Java编程的一些细节还是稍微有点总结,正好根据云时代架构中<Java高级开发必会的50个性能优化的细节(珍藏版)>来叙述一些我和里面的点比较相符 ...
- 第九届蓝桥杯省赛c/c++真题明码题解答案,另类excel解法思路
直到快比赛才重视起之前学校给报了蓝桥杯,且这段时间一直在做Python,所以没做什么准备. 赛场上做这道题时连反码补码的知识点都记混,所以直接用了excel做这道题目,分享下做题思路.及题解. 标题: ...
- MVC 中引用Angularjs
首先在Maname NuGet Packages中 安装相应的包,我用的是作者为 AngualrJS Team的 随后在相应的Scripts中会出现对应文件. 如果只在某一个页面中使用Angualrj ...
- Swift Json解析与model互转
Json的解码与编码操作,这里使用swift自带的类JSONDecoder 和 JSONEncoder 1.基础处理 如果你的 JSON 数据结构和你使用的 Model 对象结构一致的话,那么解析过程 ...
- 面向对象-main函数
面向对象-main函数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.编写main函数测试代码 /** * * @author 尹正杰 * */ public class ...