C. Given Length and Sum of Digits... (贪心)
https://codeforces.com/problemset/problem/489/C
C. Given Length and Sum of Digits...
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).
Examples
input
2 15
output
69 96
input
3 0
output
-1 -1
题意:
给定一个数m 和 一个长度s,计算最大和最小在s长度下的十进制各位数和为m的值
注意如果无法生成,即输出两个-1
题解:
有个小坑,在输出最大值时得注意下k 是否为 0
#include<bits/stdc++.h>
using namespace std;
int main()
{
int m, s, i, k;
cin >> m >> s;
if (s < 1 && m>1 || s > m * 9)
cout << -1 << " " << -1 << endl;
else {
for (i = m - 1, k = s; i >= 0; i--) {
int j = max(0, k - 9 * i);
if (j == 0 && i == m - 1 && k) j = 1;
cout << j;
k -= j;
}
cout << ' ';
for (i = m - 1, k = s; i >= 0; i--) {
int j = min(9, k);
cout << j;
k -= j;
}
}
}
C. 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 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 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...
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的最大和最小整数,不包括前导0(比如说003是非法的),但0是可以的. 分析: 这题是用贪心来做的,同样是m位数,前面的数字越大这个数就越大. 所以写一个can( ...
- Codeforces 489C Given Length and Sum of Digits...
m位长度,S为各位的和 利用贪心的思想逐位判断过去即可 详细的注释已经在代码里啦~ //#pragma comment(linker, "/STACK:16777216") //f ...
- Codeforces Round #667 (Div. 3) D. Decrease the Sum of Digits (贪心)
题意:给你一个正整数\(n\),每次可以对\(n\)加一,问最少操作多少次是的\(n\)的所有位数之和不大于\(s\). 题解:\(n\)的某个位置上的数进位,意味这后面的位置都可以被更新为\(0\) ...
- codeforces 489C.Given Length and Sum of Digits... 解题报告
题目链接:http://codeforces.com/problemset/problem/489/C 题目意思:给出 m 和 s,需要构造最大和最小的数.满足长度都为 m,每一位的数字之和等于 s. ...
随机推荐
- 阿里云服务器docker系统 BUG
阿里云服务器docker系统 BUG购买了阿里云新加坡区的轻量服务器,安装的是docker专用系统,故障现象:docker镜像下载后,docker网络不通,docker端口不通,网络一直不通,通过防火 ...
- 【Android】Android Bmob后端云配置
简介 开发一个具有网络功能的应用,在Bmob移动应用云存储平台中,只需要注册一个账号,就可以实现申请创建任意多个数据库,获得对应的key,下载对应版本的SDK,并嵌入到移动应用中,调用存取的KPI,进 ...
- 【Javaweb】html frame标签的使用 | 导航栏右侧内容的实现
问题的产生: 是我和同伴做了一个导航栏,但是我们不知道怎么实现右侧内容的切换 然后我们查了很多资料,但是有一些是垂直的,但是就如图可见,我们是水平的,那么怎么实现水平切换呢? 上网看到了frame标签 ...
- 大数据开发要学什么java还是python?
在大数据开发领域,Java和Python都是备受青睐的编程语言.它们分别具有各自独特的特点和优势,在大数据处理方面也有不同的应用场景. 以下是对Java和Python在大数据开发中的应用.优势以及学习 ...
- 当数字孪生系统接入CesiumJS,将为智慧城市项目带来怎样的改变?
数字孪生系统接入CesiumJS的契机,正是智慧城市项目的需要.因为许多智慧城市项目中包含了大量地形.倾斜摄影.DOM.DEM等GIS数据,那么为了能够在数字孪生系统中导入这些GIS数据,同时让这些数 ...
- Wifi BSSID获取
代码很简单,通过wifiManager 获取wifiinfo,从而获取bssid, public static String getWifiSSID(Context context) { String ...
- 一文带你深入理解K8s-Pod的意义和原理
本文分享自华为云社区<深入理解K8s-Pod的意义和原理>,作者:breakDawn. 在Kubernetes概念中,有以下五种概念: 容器container:镜像管理的最小单位 生产任务 ...
- 文心一言 VS 讯飞星火 VS chatgpt (158)-- 算法导论12.3 5题
五.用go语言,假设为每个结点换一种设计,属性 x.p 指向 x 的双亲,属性 x.succ 指向 x 的后继.试给出使用这种表示法的二叉搜索树 T 上 SEARCH.INSERT 和DELETE 操 ...
- 【程序员的福音】一款C#开源的GitHub加速神器
前言 作为一个程序员你是否会经常会遇到GitHub无法访问(如下无法访问图片),或者是访问和下载源码时十分缓慢就像乌龟爬行一般.之前有尝试过手动修改host文件来解决网站的访问问题,以及更换网络但还是 ...
- Triple DES 加密解密技术解析
摘要:本文介绍了Triple DES加密解密技术,通过实例演示了加密和解密过程,并对算法原理进行了简要分析.同时,探讨了Triple DES在现代信息安全领域的应用和局限性. 3DES(Triple ...