-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1462C - Unique Number.cpp
66 lines (49 loc) · 1.07 KB
/
1462C - Unique Number.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
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=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=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
inputCopy
4
1
5
15
50
outputCopy
1
5
69
-1
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int x;
cin >> x;
if (x > 45) {
cout << -1 << "\n";
} else {
string res = "";
vector <bool> used(10, false);
for (int i = 9; i >= 1; i--) {
if (used[i] == false && x >= i) {
used[i] = true;
res = to_string(i) + res;
x -= i;
}
}
cout << res << "\n";
}
}
return 0;
}