题意:

找出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...的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces 489C Given Length and Sum of Digits...

    m位长度,S为各位的和 利用贪心的思想逐位判断过去即可 详细的注释已经在代码里啦~ //#pragma comment(linker, "/STACK:16777216") //f ...

  8. codeforces 489C.Given Length and Sum of Digits... 解题报告

    题目链接:http://codeforces.com/problemset/problem/489/C 题目意思:给出 m 和 s,需要构造最大和最小的数.满足长度都为 m,每一位的数字之和等于 s. ...

  9. CF 277.5 C.Given Length and Sum of Digits.. 构造

    #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #incl ...

随机推荐

  1. Ruby 语法快速入门

    作用域:指的是类,模块,方法 常量:无需指定类型,全大写 FANS = 100 puts "We have" + FANS.to_s + "fans" 变量 局 ...

  2. ubuntu下格式化内存当硬盘使的小实验

    内存虚拟硬盘(ramdisk)是指通过软件技术,将物理内存进行分割,将一部分内存通过虚拟技术转变为硬盘以较大幅度提升计算机数据读取速度和保护硬盘. 在ubuntu下的dev下有ram相关的文件,这些文 ...

  3. 模仿开发H5游戏,看你有多色

    开发记录 前言 之前跟着慕课网学习开发H5小游戏开心鱼,勾起我的兴趣. 在写代码的过程中,不怎么会遇到问题.虽然代码是亲手敲出来的,但是由于并没有对游戏的整体思路,所以并不知道开发与优化的过程. 为了 ...

  4. ios 开发常用快捷键

    CTRL + K 删除一行,尽量在行首处使用: CMD+ /  注释,取消注释 CMD + R  运行 CMD + . 停止运行 CMD + F   普通搜索 CMD + CTRL + ↑/↓ 切换头 ...

  5. CF Gym100548 K Last Defence 解题报告

    先特判掉特殊情况: $a=b,Ans=2$ $ab=0,a+b>0,Ans=2$ $a=b=0,Ans=1$ 考虑剩下的非特殊情况.记$Solve(a,b)$为数列中除了$a,b$外的不同的数的 ...

  6. [转载]C# winform登陆框验证码的实现

    验证码技术已愈来愈成熟,从最初的数字.字母.字符.汉字已经到目前的语言,其应用也甚广,之前大多数只有在网站上可以看到,现在在一些客户端软件也经常可见(比如证券相关软件).之前做的一个基于 C# 客户端 ...

  7. PHP流程控制的替代语法

    准备做个wordpress的主题,结果看到了如下的语法: <div id="primary" class="content-area"><ma ...

  8. css table表格无法调整宽度问题分析

    1.在网上查找了相关问题,有的说表格设置了背景图片,把原来的宽度撑开了,导致无法变窄~! 在项目中,原来美工设计的页面,设置了一个块的样式class="title",现在有一段ht ...

  9. [转载]Spring Autowire自动装配介绍

    转自: http://www.cnblogs.com/zhishan/p/3190757.html 在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型 ...

  10. java数据结构学习(一)之二分查找

      二分查找法与我们在孩童时期玩过的猜数的游戏一样,这个游戏里一个朋友会让你猜他正想的一个1至100的数,当你猜了一个数后,他会告诉你三种选择的一个:你猜的比她想的大,或小,或猜中了.为了能用最少的次 ...