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 }
原文地址:https://www.cnblogs.com/towerbird/p/11257227.html
时间: 2024-09-30 14:17:50