MCLONUM - Closest Number
题意翻译
# 问题描述:
有两个没有前导0的n位十进制数A和B。我们需要找到两个最接近A的n位数(第1个大于等于A,第二小于A),包含B的所有数字。
例如,如果A=3022,B=1232,采用B的数字,我们可以得到以下4位数:1223,1232,1322,2123,2132,2213,2231,2312,2321,3122,3212和3221。
最小的大于等于A是3122,最大小于A的是2321。
如果A=1232,而B=3022,可能的4位数是2023,2032,2203,2230,2302,2320,3022,3202和3220。
最小的大于等于A是3122,最大小于A的是2023。不存在小于A的数。
编写程序,对于给定的A和B,找出最接近A的包含B的所有数字的数,或者确定其中一个是不存在的。
# 输入格式:
输入两行,都是没有前导0的n(1 n 60)位正整数,第一行是A,第二行是B。
# 输出格式:
输出两行,每行一个十进制数。
第1行是最小的大于等于A的没有前导0的,包含B所有数字的n位十进制数。如果这样的数不存在,则输出0。
第2行是最大的小于A的没有前导0的,包含B所有数字的n位十进制数。如果这样的数不存在,则输出0。
# 样例输入输出:
```
Closest1.in
3075
6604
Closest1.out
4066
0
Closest2.in
3000203
4562454
Closest2.out
4244556
2655444
```
题目描述
[English](/problems/MCLONUM/en/) [Vietnamese](/problems/MCLONUM/vn/) ```
Consider two n-digit positive decimal integers A and B with no leading
zeroes. We need to find the two closest to A n-digit numbers (the
first one – greater or equal to A, the other – strictly less than A),
with decimal writings containing all the digits of B in some order.
For example if A=3022 and B=1232, using B’s digits we can obtain
the following 4-digit numbers: 1223, 1232, 1322, 2123, 2132, 2213,
2231, 2312, 2321, 3122, 3212 and 3221. The least number greater
or equal to A obtained by B’s digits is 3122, and the biggest one,
strictly less then A is 2321. If A=1232 and B=3022, the possible numbers
are 2023, 2032, 2203, 2230, 2302, 2320, 3022, 3202 and
3220. The least number greater or equal to A obtained by B’s digits
is 2023, and there is no number less than A.
Write a program closest to find these “closest to A” numbers for
given A and B, or to determine that one of them does not exist.
```
输入输出格式
输入格式
```
Two lines are read from the standard input, each of them containing an
n-digit positive integer with no leading zeroes, with A read from the
first, and B read from the second line (1≤n ≤ 60).
```
```
SAMPLE INPUT
Example 1 Example 2
3075 3000203
6604 4562454
```
输出格式
```
Write to the standard output:
- Line 1: the least n-digit number with no leading zeroes,
not less than A, containing all the digits of B in some order.
If such number does not exist, the output should be 0.
- Line 2: the biggest n-digit number with no leading zeroes, less
than A, containing all the digits of B in some order. If such number
does not exist, the output should be 0.
SAMPLE OUTPUT
Example 1 Example 2
4066 4244556
0 2655444
```
**Problem for kid - Please, think like kid.**