C. Unique Number
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a positive number 𝑥x. Find the smallest positive integer number that has the sum of digits equal to 𝑥x and all digits are distinct (unique).
Input
The first line contains a single positive integer 𝑡t (1≤𝑡≤501≤t≤50) — the number of test cases in the test. Then 𝑡t test cases follow.
Each test case consists of a single integer number 𝑥x (1≤𝑥≤501≤x≤50).
Output
Output 𝑡t answers to the test cases:
- if a positive integer number with the sum of digits equal to 𝑥x and all digits are different exists, print the smallest such number;
- otherwise print -1.
Example
input
Copy
4 1 5 15 50
output
Copy
1 5 69 -1
简单的 一层嵌套可以不写
然而多层的嵌套必须要写,不然滑铁卢
以下分别是写嵌套和不写嵌套的结果:
include <iostream>
using namespace std;
int t, x, i;
int main()
{
cin >> t;
while( t-- )
{
cin >> x;
if ( x <= 9)
printf("%d\n", x);
else if (x >= 10 && x <= 17)
{
for(i = 1; i < 9; ++i)
{
if (i + 9 == x)
cout << i * 10 + 9 << endl;
}
}
else if (x >= 18 && x <= 24)
{
for(i = 1; i < 8; ++i)
{
if (i + 8 + 9 == x)
cout << i * 100 + 89 << endl;
}
}
else if (x >= 25 && x <= 30){
for(i = 1; i < 7; ++i)
{
if (i + 7 + 8 + 9 == x)
cout << i * 1000 + 789 << endl;
}
}
else if (x >= 31 && x <= 35)
{
for(i = 1; i < 6; ++i)
{
if (i + 6 + 7 + 8 + 9 == x)
cout << i * 10000 + 6789 << endl;
}
}
else if (x >= 36 && x <= 39)
{
for(i = 1; i < 5; ++i)
{
if (i + 5 + 6 + 7 + 8 + 9 == x)
cout << i * 100000 + 56789 << endl;
}
}
else if (x >= 40 && x <= 42)
{
for(i = 1; i < 4; ++i)
{
if (i + 4 + 5 + 6 + 7 + 8 + 9 == x)
cout << i * 1000000 + 456789 << endl;
}
}
else if (x >= 43 && x <= 44)
{
for(i = 1; i < 3; ++i)
{
if(i + 3 + 4 + 5 + 6 + 7 + 8 + 9 == x)
cout << i * 10000000 + 3456789 << endl;
}
}
else if (x == 45)
{
cout<<123456789<<'\n';
}
else {cout << "-1" << endl;}
}
return 0;
}
不写嵌套:
include <iostream>
using namespace std;
int t, x, i;
int main()
{
cin >> t;
while( t-- )
{
cin >> x;
if ( x <= 9)
printf("%d\n", x);
else if (x >= 10 && x <= 17)
{
for(i = 1; i < 9; ++i)
{
if (i + 9 == x)
cout << i * 10 + 9 << endl;
}
}
else if (x >= 18 && x <= 24)
{
for(i = 1; i < 8; ++i)
{
if (i + 8 + 9 == x)
cout << i * 100 + 89 << endl;
}
}
else if (x >= 25 && x <= 30){
for(i = 1; i < 7; ++i)
{
if (i + 7 + 8 + 9 == x)
cout << i * 1000 + 789 << endl;
}
}
else if (x >= 31 && x <= 35)
{
for(i = 1; i < 6; ++i)
{
if (i + 6 + 7 + 8 + 9 == x)
cout << i * 10000 + 6789 << endl;
}
}
else if (x >= 36 && x <= 39)
{
for(i = 1; i < 5; ++i)
{
if (i + 5 + 6 + 7 + 8 + 9 == x)
cout << i * 100000 + 56789 << endl;
}
}
else if (x >= 40 && x <= 42)
{
for(i = 1; i < 4; ++i)
{
if (i + 4 + 5 + 6 + 7 + 8 + 9 == x)
cout << i * 1000000 + 456789 << endl;
}
}
else if (x >= 43 && x <= 44)
{
for(i = 1; i < 3; ++i)
{
if(i + 3 + 4 + 5 + 6 + 7 + 8 + 9 == x)
cout << i * 10000000 + 3456789 << endl;
}
}
else if (x == 45)
{
cout<<123456789<<'\n';
}
else {cout << "-1" << endl;}
}
return 0;
}
end!