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 }

原文地址:https://www.cnblogs.com/towerbird/p/11257227.html

时间: 2024-09-30 14:17:50

C. Given Length and Sum of Digits...的相关文章

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, sizeo

Codeforces Round #277.5 (Div. 2)C. Given Length and Sum of Digits...(贪心)

传送门 Description 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 d

Codeforces Round #277.5 (Div. 2)——C贪心—— 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 with

Codeforces 489C Given Length and Sum of Digits...

m位长度,S为各位的和 利用贪心的思想逐位判断过去即可 详细的注释已经在代码里啦~ //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <cstring> #include <cmath> #include <stack> #include <queu

Codeforces Round #277.5 (Div. 2) C Given Length and Sum of Digits...

大大的传送门:就是这里 这一道卡在了特判的  1   0 本来输出 0  0 输出  -1    -1了,就错了,, 这道题就是一个恨好的贪心,往大了贪 下面是代码 #include <cstdio> #include <cstring> #include <vector> using namespace std; int n,m; int ans[110]; int main(){ scanf("%d%d",&n,&m); mems

贪心/CodeForces 489C Given Length and Sum of Digits...

1 #include<cstdio> 2 using namespace std; 3 int m,s; 4 int main() 5 { 6 scanf("%d%d",&m,&s); 7 if (s==0 && m==1) printf("0 0\n"); 8 else if (s==0) printf("-1 -1\n"); 9 else if (s>m*9) printf("-1

codeforces 489C.Given Length and Sum of Digits... 解题报告

题目链接:http://codeforces.com/problemset/problem/489/C 题目意思:给出 m 和 s,需要构造最大和最小的数.满足长度都为 m,每一位的数字之和等于 s.如果构造不出来,输出 -1 -1.否则输出最小和最大且符合条件的数. 想了两个多小时,发现想错了方向...... /****************************************** 首先把不能得到最小数和最大数的情况揪出来. 第二组测试数据 3 0 有提示,s = 0 且 m >

8504. Sum of Digits

Sum of Digits Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a n

Sum of Digits is Prime

Sum of Digits is Prime Daoyi Peng August 19, 2014 For an integer $q\geqslant2$ let $s_q(n)$ denote the $q$-ary sum-of-digits function of a non-negative integer $n$, that is, if $n$ is given by its $q$-ary digits expansion $n=\sum\limits_{k=0}^{r} a_k