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 ...
随机推荐
- android开发 PopupWindow 设置充满屏幕
View qrcode_view = this.getLayoutInflater().inflate(R.layout.taskdetail_qrcode,null); final PopupWin ...
- IIS Express start introduction and applicationHost modification
1. First you need create a web project in VS 2. When you finish your project, click start then IIS E ...
- 1304: [CQOI2009]叶子的染色 - BZOJ
Description给一棵m个结点的无根树,你可以选择一个度数大于1的结点作为根,然后给一些结点(根.内部结点和叶子均可)着以黑色或白色.你的着色方案应该保证根结点到每个叶子的简单路径上都至少包含一 ...
- Eclipse 安装热部署JRebel
开发环境 sts-3.7.2.RELEASE 安装步骤 1.打开应市场 2.搜索JRebel并进行下载 3.下载完成后点击JReble Configuation进入
- 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!
// test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- PAT-乙级-1051. 复数乘法 (15)
1051. 复数乘法 (15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 复数可以写成(A + Bi)的常规 ...
- [设计模式] 11 享元模式 Flyweight
转 http://blog.csdn.net/wuzhekai1985/article/details/6670298 问题 在面向对象系统的设计何实现中,创建对象是最为常见的操作.这里面就有一个问题 ...
- Implicitly Typed Local Variables
Implicitly Typed Local Variables It happens time and time again: I’ll be at a game jam, mentoring st ...
- HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)
一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...
- hdu 2177 取(2堆)石子游戏 博弈论
由于要输出方案,变得复杂了.数据不是很大,首先打表,所有whthoff 的奇异局势. 然后直接判断是否为必胜局面. 如果必胜,首先判断能否直接同时相减得到.这里不需要遍历或者二分查找.由于两者同时减去 ...