码迷,mamicode.com
首页 > 其他好文 > 详细

CF 489 C Given Length and Sum of Digits... 贪心

时间:2017-08-16 18:23:37      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:bsp   memory   const   problems   lin   cat   inf   最大   spec   

题目链接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 }

题目:

C. Given Length and Sum of Digits...
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ms (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 15
output
69 96
input
3 0
output
-1 -1

CF 489 C Given Length and Sum of Digits... 贪心

标签:bsp   memory   const   problems   lin   cat   inf   最大   spec   

原文地址:http://www.cnblogs.com/bolderic/p/7374656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!