1400*C. Given Length and Sum of Digits...(贪心)

Problem - 489C - Codeforces

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
int m,s,a[110];
signed main(){
	scanf("%d%d",&m,&s);
	if(s>m*9) puts("-1 -1");
	else if(s==0&&m>1) puts("-1 -1");
	else if(s==0&&m==1) puts("0 0");
	else{
		int t=s;
		t-=1;
		a[1]=1;
		for(int i=m;i>=1;i--){
			if(t>=9){
				a[i]+=9;
				t-=9;
			}
			else{
				a[i]+=t;
				break;
			}
		}
		for(int i=1;i<=m;i++) cout<<a[i];
		cout<<" ";
		t=s;
		memset(a,0,sizeof a);
		for(int i=1;i<=m;i++){
			if(t>=9){
				a[i]+=9;
				t-=9;
			}
			else{
				a[i]+=t;
				break;
			}
		}
		for(int i=1;i<=m;i++) cout<<a[i];
	}
	return 0;
}
相关推荐
Tony Bai3 小时前
高并发后端:坚守 Go,还是拥抱 Rust?
开发语言·后端·golang·rust
wjs20244 小时前
Swift 类型转换
开发语言
前端小L4 小时前
贪心算法专题(十):维度权衡的艺术——「根据身高重建队列」
javascript·算法·贪心算法
方得一笔4 小时前
自定义常用的字符串函数(strlen,strcpy,strcmp,strcat)
算法
秃了也弱了。4 小时前
python实现定时任务:schedule库、APScheduler库
开发语言·python
weixin_440730504 小时前
java数组整理笔记
java·开发语言·笔记
Dillon Dong4 小时前
从C到Simulink: 使用STM32硬件支持包后为什么还不支持PC仿真ARM建模程序
c语言·stm32·simulink
Xの哲學4 小时前
Linux SMP 实现机制深度剖析
linux·服务器·网络·算法·边缘计算
Thera7774 小时前
状态机(State Machine)详解:原理、优缺点与 C++ 实战示例
开发语言·c++
wuk9985 小时前
使用PCA算法进行故障诊断的MATLAB仿真
算法·matlab