C. Given Length and Sum of Digits...
Posted towerbird
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C. Given Length and Sum of Digits...相关的知识,希望对你有一定的参考价值。
http://codeforces.com/contest/489/problem/C
大数就是从最高位,能大就大;小数就是从最低位,能小就小,再处理下最高位为0的情况。
无结果无非一个sum太小,min全为0,一个sum太大,全为9还有剩
1 public class Main
2 public static void main(String[] args)
3 Scanner io = new Scanner(System.in);
4 int len = io.nextInt(), sum = io.nextInt();
5 int[] max = new int[len + 1];
6 int[] min = new int[len + 1];
7
8 int sum1 = sum, sum2 = sum;
9 for (int i = 1; i <= len; sum1 -= max[i++]) max[i] = Math.min(9, sum1);
10 for (int i = len; i >= 1; sum2 -= min[i--]) min[i] = Math.min(9, sum2);
11
12 //min最高位最少是1
13 if (min[1] == 0) for (int i = 2; i <= len; i++)
14 if (min[i] > 0)
15 min[i]--;
16 min[1]++;
17 break;
18
19
20 //sum太小:min是否全为0
21 int nomin = 1;
22 for (int i = 1; i <= len; i++) if (min[i] != 0) nomin = 0;
23
24 //sum太大:sum1、sum2没减完
25 if ((nomin == 1 && len != 1) || sum1 != 0 || sum2 != 0) System.out.println("-1 -1");
26 else
27 for (int i = 1; i <= len; i++) System.out.print(min[i]);
28 System.out.print(" ");
29 for (int i = 1; i <= len; i++) System.out.print(max[i]);
30
31
32
以上是关于C. Given Length and Sum of Digits...的主要内容,如果未能解决你的问题,请参考以下文章
C. Given Length and Sum of Digits...1400 / 贪心
Codeforces Round #277.5 (Div. 2)C. Given Length and Sum of Digits...(贪心)
2021-8-28 Given Length and Sum of Digits
CF 489 C Given Length and Sum of Digits... 贪心