CF 489 C Given Length and Sum of Digits... 贪心
Posted EricJeffrey
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF 489 C Given Length and Sum of Digits... 贪心相关的知识,希望对你有一定的参考价值。
题目链接:http://codeforces.com/problemset/problem/489/C
题目大意:给定位数和各个位的和,问满足条件数字的最大值,最小值。
解题思路:模拟即可。主要是细节判断。
代码:
1 const int inf = 0x3f3f3f3f; 2 const int maxn = 1e2 + 5; 3 int m, s; 4 char ans1[maxn], ans2[maxn]; 5 6 void solve(){ 7 memset(ans1, 0, sizeof(ans1)); 8 memset(ans2, 0, sizeof(ans1)); 9 10 if((s == 0 && m > 1) || s > m * 9) { 11 printf("-1 -1\n"); 12 return; 13 } 14 int x = s; 15 for(int i = 1; i <= m; i++){ 16 int u = (m - i) * 9; 17 if(u < x) { 18 ans1[i] = (x - u + ‘0‘); 19 x = u; 20 } 21 else if(u == x){ 22 if(i == 1) { 23 if(x > 0) { 24 ans1[i] = ‘1‘; 25 x -= 1; 26 } 27 else ans1[i] = ‘0‘; 28 } 29 else { 30 ans1[i] = ‘0‘; 31 } 32 } 33 else if(u > x) { 34 if(i == 1) { 35 ans1[i] = ‘1‘; 36 x -= 1; 37 } 38 else ans1[i] = ‘0‘; 39 } 40 } 41 x = s; 42 for(int i = 1; i <= m; i++){ 43 if(x >= 9) { 44 ans2[i] = ‘9‘; 45 x -= 9; 46 } 47 else{ 48 ans2[i] = (x + ‘0‘); 49 x = 0; 50 } 51 } 52 53 printf("%s %s\n", ans1 + 1, ans2 + 1); 54 } 55 int main(){ 56 scanf("%d %d", &m, &s); 57 solve(); 58 }
题目:
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).
2 15
69 96
3 0
-1 -1
以上是关于CF 489 C Given Length and Sum of Digits... 贪心的主要内容,如果未能解决你的问题,请参考以下文章
CodeForces 489C Given Length and Sum of Digits...
C. Given Length and Sum of Digits...
C. Given Length and Sum of Digits...1400 / 贪心
C. Given Length and Sum of Digits... (贪心)
2021-8-28 Given Length and Sum of Digits
Codeforces Round #277.5 (Div. 2)C. Given Length and Sum of Digits...(贪心)