CodeForces 489C Given Length and Sum of Digits... (贪心)
Given Length and Sum of Digits...
题目链接:
http://acm.hust.edu.cn/vjudge/contest/121332#problem/F
Description
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).
Sample Input
Input
2 15
Output
69 96
Input
3 0
Output
-1 -1
题意:
给出m和s,分别找出最大和最小的非负整数,满足数位长度为m,数位和为s;
题解:
直接贪心枚举每一位就可以了;
最小:能放0就放0; (首位不能是0)
最大:能放9就放9;
WA一次:题目特地强调了非负整数;
那么考虑s为0的情况,并不是全为-1 -1;
当m=1 s=0时应输出0 0;
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 150
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int m,s;
int main(int argc, char const *argv[])
{
//IN;
int n,s;
while(scanf("%d %d", &n, &s) != EOF)
{
if(n*9<s) {printf("-1 -1\n");continue;}
if(!s) {
if(n==1) printf("0 0\n");
else printf("-1 -1\n");
continue;
}
int cur = s;
for(int i=n; i>=1; i--) {
int flag = 0;
if(i==n) flag = 1;
if(9*(i-1) <= cur-flag) {
printf("%d", cur-9*(i-1));
for(int j=1; j<=i-1; j++)
printf("9");
break;
}
if(flag) {
printf("1"); cur-=1;
} else {
printf("0");
}
}
printf(" ");
cur = s;
for(int i=n; i>=1; i--) {
if(9 >= cur) {
printf("%d", cur);
for(int j=1; j<=i-1; j++)
printf("0");
break;
}
printf("9"); cur-=9;
}
printf("\n");
}
return 0;
}
CodeForces 489C Given Length and Sum of Digits... (贪心)的更多相关文章
- 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为各位的和 利用贪心的思想逐位判断过去即可 详细的注释已经在代码里啦~ //#pragma comment(linker, "/STACK:16777216") //f ...
- 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( ...
- Codeforces Round #667 (Div. 3) D. Decrease the Sum of Digits (贪心)
题意:给你一个正整数\(n\),每次可以对\(n\)加一,问最少操作多少次是的\(n\)的所有位数之和不大于\(s\). 题解:\(n\)的某个位置上的数进位,意味这后面的位置都可以被更新为\(0\) ...
随机推荐
- Android中使用广播机制退出多个Activity
谷歌百度一下,Android中退出多个Activity的方法,大家讨论的很多. 在实习的时候,看到公司的项目退出多个Activity,是采用LinkedList方法,毕业设计的时候,也参照了那种方法. ...
- MySql和Hibernate中关于cascade的用法
数据库里的cascade的用法,Mysql和Hibernate里面是不相同. 在数据库里,进行增加.修改.删除记录的时候,经常会涉及到父子关系的表. 例如:有省份表和城市表,其中城市表有一个外键pro ...
- UVa 10837 (欧拉函数 搜索) A Research Problem
发现自己搜索真的很弱,也许做题太少了吧.代码大部分是参考别人的,=_=|| 题意: 给出一个phi(n),求最小的n 分析: 回顾一下欧拉函数的公式:,注意这里的Pi是互不相同的素数,所以后面搜索的时 ...
- 英文 数字 不换行 撑破div容器
我们在div等容器 中,如果规定了宽度,并且里面的内容不是全英文或者全数字是OK的,会自动换行,但是如果是全数字或者是全英文,则会撑破容器,如图 解决方法 word-wrap:break-wo ...
- tomcat启动中提示 - consider increasing the maximum size of the cache
tomcat启动过程中提示: org.apache.catalina.webresources.Cache.getResource Unable to add the resource at xxx ...
- BZOJ 1610 连线游戏
BZOJ不允许除以0. #include<iostream> #include<cstdio> #include<cstring> #include<cstd ...
- linux lnmp编译安装
关闭SELINUX vi /etc/selinux/config #SELINUX=enforcing #注释掉 #SELINUXTYPE=targeted #注释掉 SELINUX=disabled ...
- jquery插件——日历控件
今天在网上有看到一个jquery插件——日历控件,不过之前也在柯乐义的网站上看到了(http://keleyi.com/ 推荐下) 这个插件看着比较大气,所以干脆也分享下,以后自己也好用一点儿 1.页 ...
- ubuntu鼠标突然不能使用的解决方法
今天发现鼠标(usb即插即用)不能用了,最后发现需要接通充电才可以!!!用电池的时候居然不可以用鼠标?
- MyBatis 如何接收参数
MyBatis的mapper接口不需要自己实现,框架会自动帮我们实现,到时候直接调用就可以了.定义的mapper接口中的方法可以有多个参数吗?答案是肯定.在Ibatis时代是自己通过代码实现如何调用x ...