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 ...
随机推荐
- 《C++Primer》复习——with C++11 [4]
考虑到STL的掌握主要靠的是练习,所以对于STL这部分,我把书中的练习都做一遍,加深印象.这些练习是第9.10.11.17章的,分别是顺序容器.泛型算法和关联容器等. ——10月22日 /*----- ...
- Flv 视频格式(转)
最近要用到flv,整理了一些flv格式的资料,供参考. flv文件主要由两部分组成:header和body. 1.header header部分记录了flv的类型.版本等信息,是flv的开头,一般都差 ...
- C++对MS SQL Server的操作
今天因为在做一份C++的期末作业,突然想用C++来链接数据库,实现数据的重复利用,所以就作死去百度搜了一下. 更巧的事情是,一搜居然还有很多搜索结果,然后就照着做了. 做的过程很艰辛,就不一一诉说了, ...
- Matlab中min/max函数的误解
1.C= min(a):返回最小值:我原来以为如果a是行向量,min(a)返回a本身,因为我记得min(a,1)是按列找最小,这是默认的.doc min发现,只要a是向量,那么返回最小值. 2.C= ...
- spring mvc 注解 annot失效
如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将service bean配置到xml文件中才行 这个问题是由于问答上有解决方案 引用 这个问题很经典了 在主容器中 ...
- MongoDB 性能优化五个简单步骤
MongoDB 一直是最流行的 NoSQL,而根据 DB-Engines Ranking 最新的排行,时下 MongoDB 已经击败 PostgreSQL 跃居数据库总排行的第四位,仅次于 Oracl ...
- Root resource classes
Overview A root resource class is the entry point into a JAX-RS implemented RESTful Web service. It ...
- 常见的排序算法之Java代码解释
一 简要介绍 一般排序均值的是将一个已经无序的序列数据重新排列成有序的 常见的排序分为: 1 插入类排序 主要就是对于一个已经有序的序列中,插入一个新的记录.它包括:直接插入排序,折半插入排序和希尔排 ...
- POJ 2029 Get Many Persimmon Trees(DP||二维树状数组)
题目链接 题意 : 给你每个柿子树的位置,给你已知长宽的矩形,让这个矩形包含最多的柿子树.输出数目 思路 :数据不是很大,暴力一下就行,也可以用二维树状数组来做. #include <stdio ...
- Android 使用系统的Activity播放音频文件 intent
Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Inten ...