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权限安全(5)组件的android:exported属性
Android四大组件都有 android:exported 属性 android:exported="true" 时 表示该组件是公开的,其它组件可以访问这个组件 android ...
- textbox不支持Ctrl+A
http://stackoverflow.com/questions/5885739/why-are-some-textboxes-not-accepting-control-a-shortcut-t ...
- Provider Pattern提供者模式和策略模式
http://www.codeproject.com/Articles/18222/Provider-Pattern Introduction Provider pattern is one of t ...
- [POJ2377]Bad Cowtractors(最大生成树,Kruskal)
题目链接:http://poj.org/problem?id=2377 于是就找了一道最大生成树的AC了一下,注意不连通的情况啊,WA了一次. /* ━━━━━┒ギリギリ♂ eye! ┓┏┓┏┓┃キリ ...
- uvaIrrelevant Elements
唯一分解定理. 可以看出在最后每个a的系数是杨辉三角的第n行. 但是不能递推,否则会tle. 就从C(n-1,0)开始乘n-k再除以k.记录下每个的系数,如果该项系数小于m就代表和答案有关. 代码里的 ...
- Asp.net 后台添加Meta标签方法
Asp.net 后台添加Meta标签方法包括keywords,CSS.JS 下面是从Asp.net 后台添加CSS.JS.Meta标签的写法,我们这里写成函数方便以后使用.如果函数放在页面类中, Pa ...
- asp.net实现文件解压和压缩
C#解压RAR压缩文件(--转载--测试通过) using System; using System.Collections.Generic; using System.Text; using Sys ...
- wince和window mobile winphone
windows mobile是微软在2000年左右推出的针对移动平台的操作系统,这个系统一直使用到三年前,微软开始启用metro界面,将windows mobile改名为windows phone. ...
- 纯css3写的仿真图书翻页效果
对css3研究越深入,越觉得惊艳.css3说不上是万能的,但是它能实现的效果也超出了我的想象.它的高效率和动画效果的流畅性很多情况下能替代js的作用.个人习惯css3能实现的效果就不会用js,虽然在国 ...
- 安装--SambaServce
参考地址:快跑蚂蚁的linux之旅--redhat安装配置samba实验win共享linux主机目录 1.使用rpm -qa|grep "samba",查看samba安装包是否安装 ...