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 ...
随机推荐
- python中pandas数据分析基础3(数据索引、数据分组与分组运算、数据离散化、数据合并)
//2019.07.19/20 python中pandas数据分析基础(数据重塑与轴向转化.数据分组与分组运算.离散化处理.多数据文件合并操作) 3.1 数据重塑与轴向转换1.层次化索引使得一个轴上拥 ...
- python Scipy积分运算大全(integrate模块——一重、二重及三重积分)
python中Scipy模块求取积分的方法: SciPy下实现求函数的积分的函数的基本使用,积分,高等数学里有大量的讲述,基本意思就是求曲线下面积之和. 其中rn可认为是偏差,一般可以忽略不计,wi可 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-music
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- POJ 3983:快算24
快算24 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4791 Accepted: 2930 Description ...
- Struts+Hibernate+Spring面试题合集及答案(转)
Struts+Hibernate+Spring面试题合集及答案 Struts+Hibernate+Spring 面试题合集 1 1. Hibernate部分 2 1.1. Hibernate工作原理 ...
- gpasswd命令 gpasswd -a user_name group_name
最后一句 gpasswd命令是Linux下工作组文件/etc/group和/etc/gshadow管理工具. 语法 gpasswd(选项)(参数) 选项 -a:添加用户到组: -d:从组删除用户: - ...
- ActiveMQ持久化机制和JMS可靠消息
1.ActiveMQ持久化机制 1.1 JDBC将数据持久化到数据库 1.2 AMQ生成日志文件 1.3 KahaDB:本次磁盘生成数据文件(默认) 1.4 LevelDB:谷歌K/V数据库 1.5 ...
- 102-PHP多维数组的元素输出
<?php //定义一个三维数组 $grade=array('class1'=>array('stu1'=>array('yuwen'=>85,'shuxue'=>95, ...
- HDU 4952 Number Transformation 多校8 机智数学
哎.这个题想了好久,状态不对啊...一个大家都出的题..当时想到肯定是可以有什么规律来暴力,不用算到10的10次方 对于某个k,x.从1到k循环,每次求一个新的x,这个x要大于等于原x,并且要是i的倍 ...
- ajax异步提交 有时会出现无bug的数据处理异常-----debug没有问题,正常运行却数据处理不正确,极少机会会出现正常的处理结果
ajax 被使用时,常默认的就使用了异步处理. 当遇到后面的代码对同样的数据进行处理 或 要依赖前面ajax处理的结果时,就会导致数据处理结果不正确,未达到预期值. 且,debug时却能正常完成功能 ...