-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCPPCOM03.cpp
More file actions
57 lines (57 loc) · 866 Bytes
/
CPPCOM03.cpp
File metadata and controls
57 lines (57 loc) · 866 Bytes
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
#include<bits/stdc++.h>
using namespace std;
int n;
int a[21];
bool OK;
void init(){
cin >> n;
OK = true;
for(int i=1;i<=n;i++) a[i] = i;
}
void show(){
for(int i=1;i<=n;i++) cout << a[i] ;
cout << " ";
}
void nextGen(){
int i = n-1;
while(i > 0 && a[i] > a[i+1]) i--;
if (i > 0){
int k = n;
while(a[k] < a[i]){
k--;
}
int tmp = a[i];
a[i] = a[k];
a[k] = tmp;
int l = i+1;
int r = n;
while(l <= r){
int t = a[l];
a[l] = a[r];
a[r] = t;
l++;
r--;
}
}else OK = false;
}
int main(){
int T;
cin >> T;
while(T--){
init();
while(OK){
show();
nextGen();
}
cout << endl;
}
return 0;
}