2021-8-28 Given Length and Sum of Digits

难度 1400

题目 Codeforces:

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

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).

 

题目解析

没啥好说的,直接看代码就行了,题意很简单,就是你要给出两个数,分别是满足位数为m,各个位上数字加起来的和为s的所有数字中的最大值和最小值,如果不存在就输出“-1 -1”

 1 #include<iostream>
 2 using namespace std;
 3 typedef long long ll;
 4 int a[101], b[101];
 5 void makea(int m, int temp)
 6 {
 7     for (int i = m - 1; i >= 1; i--)
 8     {
 9         if (temp > 9)
10         {
11             a[i] = 9;
12             temp -= 9;
13         }
14         else if (temp > 1)
15         {
16             a[i] = temp - 1;
17             temp = 1;
18         }
19         else if (temp)a[i] = 0;
20     }
21     a[0] = temp;
22 }
23 void makeb(int m, int temp)
24 {
25     for (int i = 0; i < m; i++)
26     {
27         if (temp > 9)
28         {
29             b[i] = 9;
30             temp -= 9;
31         }
32         else if (temp > 0)
33         {
34             b[i] = temp;
35             temp = 0;
36         }
37         else if (!temp)b[i] = 0;
38     }
39 }
40 int main()
41 {
42     int m, s, temp; cin >> m >> s;//m是位数 s是和
43     if (m == 1 && s == 0)cout << "0 0";
44     else if (m * 9 < s || s == 0)cout << "-1 -1";
45     else
46     {
47         makea(m, s);
48         makeb(m, s);
49         for (int i = 0; i < m; i++)cout << a[i];
50         cout << " ";
51         for (int i = 0; i < m; i++)cout << b[i];
52     }
53     return 0;
54 }

 

2021-8-28 Given Length and Sum of Digits

上一篇:docker下虚拟机之间的通讯


下一篇:Mybatis二级缓存问题