728x90
반응형
- 제목
Minimum Varied Number
- 조건
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
- 문제
Find the minimum number with the given sum of digits s such that all digits in it are distinct (i.e. all digits are unique).
For example, if s=20, then the answer is 389. This is the minimum number in which all digits are different and the sum of the digits is 20 (3+8+9=20).
For the given s print the required number.
- 입력
The first line contains an integer t (1≤t≤45) — the number of test cases.
Each test case is specified by a line that contains the only integer s (1≤s≤45).
- 출력
Output
Print t integers — the answers to the given test cases.
예제 입력 1 | 4 20 8 45 10 |
예제 출력 1 | 389 8 123456789 19 |
#include <iostream>
#include <algorithm>
using namespace std;
#define endl '\n'
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tc, num;
cin >> tc;
bool usedNum[10];
while(tc--){
for(int n = 1 ; n <= 9 ; n++) usedNum[n] = false;
cin >> num;
for(int n = 9 ; n >= 1 ; n--){
if(!usedNum[n] && num >= n){
usedNum[n] = true;
num -= n;
}
}
for(int n = 1; n <= 9; n++){
if(usedNum[n]) cout << n;
}
cout << endl;
}
return 0;
}
728x90
반응형