Codeforces 489C 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.

输入

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.

输出

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

样例

输入1

2 15

输出1

69 96

输入2

3 0

输出2

-1 -1

题意

求指定位数的最小和最大的各位数之和等于指定数字的两个数字。

思路

(记得写思路)
实现思路见代码。

代码

//克服WA难,终得AC
#include"bits/stdc++.h"
#define ll long long
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define reb(i,a,b) for(ll i=a;i<=b;i++)
#define rev(i,a,b) for(ll i=a-1;i>=b;i--)
#define red(i,a,b) for(ll i=a;i>=b;i--)
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;

ll m,s;
ll digit[110];

int main() {
   
  scanf("%lld%lld",&m,&s);
  ll t=s;
  rev(i,m,0) {
   
    if(t>10) {
   
      digit[i]=9;
      t-=9;
    } else if(t>1) {
   
      digit[i]=t-1;
      t=1;
    } else
      digit[i]=0;
  }
  if(t&&digit[0]<9) {
   
    digit[0]+=t;
    t=0;
  }
  if(t||digit[0]==0&&m!=1) goto x;
  rep(i,0,m)
  cout<<digit[i];
  cout<<" ";
  t=s;
  rep(i,0,m) {
   
    if(t>9) {
   
      digit[i]=9;
      t-=9;
    } else if(t>0) {
   
      digit[i]=t;
      t=0;
    } else digit[i]=0;
  }
  rep(i,0,m) cout<<digit[i];
  cout<<endl;
  return 0;
x:
  cout<<"-1 -1"<<endl;
  return 0;
}
全部评论

相关推荐

SinyWu:七院电话面的时候问我有没有女朋友,一听异地说你赶紧分。我:???
点赞 评论 收藏
分享
小谷围鸡肉卷阿姨:+1,腾子投完一动不动
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务