codeforces489 C. Given Length and Sum of Digits...【贪心】
2018-02-10 22:52
357 查看
C. Given Length and Sum of Digits…
time limit per test1 secondmemory limit per test256 megabytes
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 15output
69 96input
3 0output
-1 -1题意: 给你一个数m,s,让你找出一个数的位数为m位,且每位加起来等于m,找出最大值和最小值
分析: 这题一看就是贪心,我们首先特判下不存在的情况,当 m*9 < s 时肯定不存在,因为最大贡献是m位都为9,还有就当s为0时,除非 m 为1,否则都是无解的情况,然后我们贪心的找出最大值,我们只要首位贪心的放较大的数即可,找最小值时,我们现在首位放一个1,然后贪心的从后往前放较大的数即可
参考代码
#include <bits/stdc++.h> using namespace std; int main() { int a,b;cin>>a>>b; if(a == 1 && b == 0) { cout<<0<<' '<<0<<endl;return 0; } if(a * 9 < b || b == 0) { cout<<-1<<' '<<-1<<endl;return 0; } string s; int B = b; for(int i = 0;i < a;i++) { if(i == 0) s += '1'; else s += '0'; } B--; for(int i = a - 1;i >= 0;i--) { int t = '9' - s[i]; if(B >= t) { s[i] = char(s[i] + t); B -= t; } else { s[i] = char(s[i] + B); break; } } cout<<s<<' '; s.clear(); for(int i = 0;i < a;i++) { s += '0'; } for(int i = 0;i < a;i++) { int t = '9' - s[i]; if(b >= t) { s[i] = char(s[i] + t); b -= t; } else { s[i] = char(s[i] + b); break; } } cout<<s<<endl; return 0; }
如有错误或遗漏,请私聊下UP,thx
相关文章推荐
- Codeforces #277.5 (Div. 2) C. Given Length and Sum of Digits...(简单贪心)
- Codeforces 489C - Given Length and Sum of Digits...(贪心)
- Codeforces Round #277.5 (Div. 2)C. Given Length and Sum of Digits...(贪心)
- Codeforces Round #277.5 (Div. 2)——C贪心—— Given Length and Sum of Digits
- Codeforces Round #277.5 (Div. 2)C. Given Length and Sum of Digits...(贪心)
- Codeforces Round #277.5(Div. 2) C. Given Length and Sum of Digits...【贪心】
- CodeForces 489C (贪心) Given Length and Sum of Digits...
- 【寒江雪】C. Given Length and Sum of Digits
- Codeforces Round #277.5 (Div. 2) C. Given Length and Sum of Digits...
- codeforces#277.5 C. Given Length and Sum of Digits
- C. Given Length and Sum of Digits...
- Codeforces-489C-Given Length and Sum of Digits...
- Codeforces Round #277.5 (Div. 2)C——Given Length and Sum of Digits...
- Codeforces Round #277.5 (Div. 2) C. Given Length and Sum of Digits...
- Given Length and Sum of Digits...---------cf水题
- Codeforces - 277.5 (Div. 2)C - Given Length and Sum of Digits...(模拟 or dfs)
- Codeforces Round #277.5 (Div. 2)-C. Given Length and Sum of Digits...
- Codeforces Round #277.5 (Div. 2)C——Given Length and Sum of Digits...
- CF 489 C Given Length and Sum of Digits...(水题)
- CF 277.5 C.Given Length and Sum of Digits.. 构造