-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10974.java
More file actions
53 lines (37 loc) · 1.04 KB
/
10974.java
File metadata and controls
53 lines (37 loc) · 1.04 KB
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
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = i+1;
}
do {
for(int a : arr) {
System.out.print(a + " ");
}
System.out.print("\n");
}
while(getNextPer(arr));
}
public static boolean getNextPer(int[] arr) {
int i;
for(i=arr.length-1; i-1 >= 0 && arr[i-1] > arr[i]; i--);
if (i-1 < 0) {
return false;
}
int j = i;
while(j < arr.length-1 && arr[i-1] < arr[j+1])
j++;
int temp = arr[i-1];
arr[i-1] = arr[j];
arr[j] = temp;
for(int k=arr.length-1; i<k; i++, k--) {
temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
return true;
}
}