-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path60getPermutation.cpp
65 lines (61 loc) · 1.48 KB
/
60getPermutation.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
#include <iostream>
#include <vector>
#include <queue>
#include <sstream>
using namespace std;
//currently total have
void getPermutationImp(int *arr, int *arrf, int n, int k, stringstream& str){
//which number should be permutated currently
if(n == 1){
for(int j = 1; j < 10; j++){
if(arr[j] != 0){
str << j;
std::cout << n << " " << j<< std::endl;
break;
}
}
return;
}
int idx = 1; // 当前应该用第几个数去枚举
while(k > idx*arrf[n-1]){
idx++;
}
int i = 0; // 当前枚举的真实的数
int cur_num = 1;
for(int j = 1; j < 10; j++){
if(arr[j] != 0){
i++;
if(i == idx){
cur_num = j;
break;
}
}
}
str << cur_num;
std::cout << n << " " << cur_num<< std::endl;
// update
if(k > (idx-1)*arrf[n-1]){
k = k- (idx-1)*arrf[n-1];
}
arr[cur_num] = 0;
getPermutationImp(arr, arrf, n-1, k, str);
}
string getPermutation(int n, int k) {
// the set of n , 1<=n <= 9
int array[10] = {0};
for(int i = 1; i <= n; i++){
array[i] = 1;
}
// the factorial of each index
int array_factorial[10] = {10};
array_factorial[1] = 1;
for(int i = 2; i <= n; i++){
array_factorial[i] = i*array_factorial[i-1];
}
stringstream str;
getPermutationImp(array, array_factorial, n, k, str);
return str.str();
}
int main(){
std::cout<<getPermutation(4, 1);
}