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\) ...
随机推荐
- varchar 保存英文中文区别。
varchar在SQL Server中是采用单字节来存储数据的,中文字符存储到SQL Server中会保存为两个字节,英文字符保存到数据库中,如果字段的类型为varchar,则只会占用一个字节,而如果 ...
- sdut 2846 Remove Trees (二分 + 贪心)
题目 和poj 上的一道题几乎一样. 题意:已知n棵树距第一棵树的距离,求删掉m棵树后的 树之间 的最小距离 的最大值. 思路:二分枚举最小的距离,注意二分的写法. #include <ios ...
- attachEvent,addEventListener事件绑定
兼容各主流浏览器的事件绑定(在同一个事件上添加多个处理函数). 1.绑定方法: //IE attachEvent(事件名, 函数) oBtn.attachEvent('onclick', aaa); ...
- BZOJ2253: [2010 Beijing wc]纸箱堆叠
题解: 其实就是求三维偏序最长链.类似于三维逆序对,我们可以用树状数组套平衡树来实现. DP方程 :f[i]=max(f[j]+1) a[j]<a[i] 我们按一维排序,另一位建立树状数组,把第 ...
- Java Web编程的主要组件技术——Hibernate入门
参考书籍:<J2EE开源编程精要15讲> Hibernate是对象/关系映射(ORM,Object/Relational Mapping)的解决方案,就是将Java对象与对象关系映射到关系 ...
- POJ:最长上升子序列
Title: http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 ...
- rtp h264注意点(FU-A分包方式说明)
前写过一篇文章,分析了h264使用rtp进行封包的格式介绍:RTP封装h264.但里面好像没有把拆分以及一些需要注意的情况说清楚,因此这里做补充,也作为自己的备忘(自己记性好像不太好). 关于时间戳, ...
- golang windows程序获取管理员权限(UAC ) via gocn
golang windows程序获取管理员权限(UAC ) 在windows上执行有关系统设置命令的时候需要管理员权限才能操作,比如修改网卡的禁用.启用状态.双击执行是不能正确执行命令的,只有右键以管 ...
- mysql open files
错误信息如下: ..... 150905 13:10:17 [ERROR] /usr/local/mysql/bin/mysqld: Can't open file: './mytest/t1.frm ...
- delete drop truncate
一.相同点 1 truncate.不带where子句的delete.drop都会删除表内的数据2 drop.truncate都是DDL语句(数据定义语言),执行后会自动提交 二.不同点 1trunca ...