-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP10974.java
More file actions
55 lines (44 loc) · 809 Bytes
/
P10974.java
File metadata and controls
55 lines (44 loc) · 809 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
package BOJ;
import java.util.*;
public class P10974 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for(int i=1;i<=n;i++){
a[i-1] = i;
System.out.print(a[i-1]+" ");
}
System.out.println();
while(next_permutation(a)){
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
}
public static boolean next_permutation(int[] a){
int i = a.length-1;
while(i>0 && a[i-1]>=a[i]){
i-=1;
}
if(i==0)
return false;
int j = a.length-1;
while(a[i-1]>=a[j]){
j-=1;
}
int temp = a[i-1];
a[i-1] = a[j];
a[j] = temp;
j = a.length-1;
while(i<j){
temp = a[i];
a[i] = a[j];
a[j] = temp;
i+=1;
j-=1;
}
return true;
}
}