C.Given Length and Sum of Digits…
C.Given Length and Sum of Digits…
time limit per test: 1 second
每次测试的时间限制:1秒
memory limit per test: 256 megabytes
每次测试的内存限制:256兆字节
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和一个非负整数s。您的任务是找到具有长度的最小和最大的数字。
m and sum of digits s.The required numbers should be non-negative integers written in the decimal base without leading zeroes.
m and sum of digits s.The required numbers should be non-negative integers written in the decimal base without leading zeroes.
lnput
输入
The single line of the input contains a pair of integers m,s(1sm≤100,0≤s≤900)—the length and the sum of the digitsof the
The single line of the input contains a pair of integers m,s(1sm≤100,0≤s≤900)—the length and the sum of the digitsof the
required numbers.
required numbers.
output
输出量
ln the output print the pair of the required non-negative integer numbers— frst the minimum possible number, then— the maximum
ln the output print the pair of the required non-negative integer numbers— frst the minimum possible number, then— the maximum
possible number. f no numbers satisfying conditions required exist, print the pair of numbers “-1 -1”(without the quotes).
可能的号码。如果不存在满足所需条件的数字,则打印数字对“-1-1”(不带引号)。
最大数和最小数都需要使每个数尽量大(从前往后,从后往前),特判首位为
int a[101] = { 0 };
int main() {
int m, s,k=0;
cin >> m >> s;
if (s < 1 && m>1 || s > m * 9)
cout << "-1 -1" << endl;
else
{
for (; k < m; ++k)
{
if (s > 8)
a[k] = 9, s -= 9;
else
k = s, s = 0;
}
sort(a, a + m);
for (int i = 0; i < m; ++i)
cout << a[i];
cout << ' ';
if (a[m - 1] == 0)
{
a[m - 1] = 0;
for (int i = m - 2; m >= 0; --i)
if (a[i] > 0)
{
--a[i];
break;
}
}
for (int i = m - 1; i >= 0; --i)
cout << a[i];
cout << endl;
}
return 0;
}