CodeForces 489C (贪心) Given Length and Sum of Digits...
题意:
找出m位且各个数位数字之和为s的最大和最小整数,不包括前导0(比如说003是非法的),但0是可以的。
分析:
这题是用贪心来做的,同样是m位数,前面的数字越大这个数就越大。
所以写一个can(int m, int s)函数,来判断是否存在一个m位数其各位数字之和为s
这里先不考虑前导0的事,代码看起来可能是这个样子的:
bool can(int m, int s)
{
return (s >= && s <= m*);
}
比如我们现在要求满足要求的最小整数,从最左边的数开始从0到9开始试,如果后面的数能够构成m-1位和为s-d的数,就开始尝试下一位。
注意:在这里就要加上不能有前导0的判定条件。
求最大数也是类似的,而且还不用考虑前导0.
#include <cstdio> using namespace std; const int maxn = + ; char a[maxn], b[maxn]; bool can(int m, int s)
{
return (s >= && s <= m*);
} int main()
{
int m, s;
scanf("%d%d", &m, &s);
if(!can(m, s))
{
puts("-1 -1");
return ;
} int sum = s, p = ;
for(int i = ; i < m; ++i)
{
for(int d = ; d < ; ++d)
{
if((i > || d > || (m == && d == )) && can(m-i-, sum-d))
{
a[p++] = '' + d;
sum -= d;
break;
}
}
}
if(p != m)
{
puts("-1 -1");
return ;
}
for(int i = ; i < p; ++i) putchar(a[i]);
putchar(' '); sum = s; p = ;
for(int i = ; i < m; ++i)
{
for(int d = ; d >= ; --d)
{
if(can(m-i-, sum-d))
{
b[p++] = '' + d;
sum -= d;
break;
}
}
}
if(p != m)
{
puts("-1 -1");
return ;
}
for(int i = ; i < m; ++i) putchar(b[i]);
puts(""); return ;
}
代码君
CodeForces 489C (贪心) Given Length and Sum of Digits...的更多相关文章
- 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 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 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 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 ...
- 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为各位的和 利用贪心的思想逐位判断过去即可 详细的注释已经在代码里啦~ //#pragma comment(linker, "/STACK:16777216") //f ...
- codeforces 489C.Given Length and Sum of Digits... 解题报告
题目链接:http://codeforces.com/problemset/problem/489/C 题目意思:给出 m 和 s,需要构造最大和最小的数.满足长度都为 m,每一位的数字之和等于 s. ...
- CF 277.5 C.Given Length and Sum of Digits.. 构造
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #incl ...
随机推荐
- Java 集合类(一)
今天我们先讲一下Collection: Collection和Collections的区别: java.util.Collection是一种java集合接口,它提供了对集合对象的基本操作通用接口方法, ...
- c++ 时间与字符串转换
.时间转字符串函数 size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timep ...
- JS中关于JS文件的引用以及问题
问题描述: 由于JSP中JS函数比较多,因此打算新建一个JS文件在JSP中引用JS文件,现在出现如下问题,JS如何引用时正确的,JS引用之后出现乱码如何解决? 问题解决: (1)JS ...
- [错误]试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)
错误原因: dll文件是在64位机下编译的,而服务器是32位机,所以无法调用 或者dll文件是在64位开发环境下下编译的,而现在的调用程序是的32位,所以无法调用 注意项目属性:
- win7 安装Oracle 10G,11G
安装 10G : 安装说明: http://wenku.baidu.com/view/a73d048bd0d233d4b14e69a8.html 按这个安装成功过. 11G R2: 在Win7 6 ...
- NOI 国家集训队论文集
鉴于大家都在找这些神牛的论文.我就转载了这篇论文合集 国家集训队论文分类 组合数学 计数与统计 2001 - 符文杰:<Pólya原理及其应用> 2003 - 许智磊:<浅谈补集转化 ...
- LightOj 1096 - nth Term (矩阵快速幂,简单)
题目 这道题是很简单的矩阵快速幂,可惜,在队内比赛时我不知什么时候抽风把模版中二分时判断的 ==1改成了==0 ,明明觉得自己想得没错,却一直过不了案例,唉,苦逼的比赛状态真让人抓狂!!! #incl ...
- hdu 1713 相遇周期
求分数的最小公倍数.对于a/b c/d 先化简为最简分数,分数最小公倍数=分子的最小公倍数/分母的最大公约数. ;}
- linux入门教程(四) 初步进入linux世界
[Linux 系统启动过程] Linux的启动其实和windows的启动过程很类似,不过windows我们是无法看到启动信息的,而linux启动时我们会看到许多启动信息,例如某个服务是否启动. Lin ...
- JVM基础学习
public class TestJVM { // 运行时数据区[方法区.堆.程序计数器.虚拟机栈.本地方法栈] private static int _1M = 1024 * 1024; publi ...