-
Notifications
You must be signed in to change notification settings - Fork 17
/
Permutations.java
70 lines (54 loc) · 1.65 KB
/
Permutations.java
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
66
67
68
69
70
// M-I
import java.io.*;
import java.util.*;
public class Main {
public static void permutations(int[] boxes, int ci, int ti){
if(ci == ti+1) {
for(int i=0; i<boxes.length; i++) System.out.print(boxes[i]);
System.out.println();
return;
}
for(int i=0; i<boxes.length; i++) {
if(boxes[i] == 0) {
boxes[i] = ci;
permutations(boxes, ci+1, ti);
boxes[i] = 0;
}
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int nboxes = Integer.parseInt(br.readLine());
int ritems = Integer.parseInt(br.readLine());
permutations(new int[nboxes], 1, ritems);
}
}
// M-II
import java.io.*;
import java.util.*;
public class Main {
// items - to Check which item has been used
public static void permutations(int cb, int tb, int[] items, int ssf, int ts, String asf){
if(cb == tb+1) {
if(ssf == ts) {
System.out.println(asf);
}
return;
}
for(int i=0; i<ts; i++) {
if(items[i] == 0) {
items[i] = 1;
permutations(cb+1, tb, items, ssf + 1, ts, asf + (i+1));
items[i] = 0;
}
}
// No Selection
permutations(cb+1, tb, items, ssf, ts, asf + 0);
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int nboxes = Integer.parseInt(br.readLine());
int ritems = Integer.parseInt(br.readLine());
permutations(1, nboxes, new int[ritems], 0, ritems, "");
}
}