- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10974.java
More file actions
Latest commit
53 lines (37 loc) · 1.04 KB
/
Copy path10974.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
importjava.util.*;
publicclassMain {
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
intn = sc.nextInt();
int[] arr = newint[n];
for (inti = 0; i < n; i++) {
arr[i] = i+1;
}
do {
for(inta : arr) {
System.out.print(a + " ");
}
System.out.print("\n");
}
while(getNextPer(arr));
}
publicstaticbooleangetNextPer(int[] arr) {
inti;
for(i=arr.length-1; i-1 >= 0 && arr[i-1] > arr[i]; i--);
if (i-1 < 0) {
returnfalse;
}
intj = i;
while(j < arr.length-1 && arr[i-1] < arr[j+1])
j++;
inttemp = arr[i-1];
arr[i-1] = arr[j];
arr[j] = temp;
for(intk=arr.length-1; i<k; i++, k--) {
temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
returntrue;
}
}