Skip to content

Commit bf52f95

Browse files
committed
codes updated.
1 parent 505781b commit bf52f95

File tree

124 files changed

+228
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+228
-1
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
public class NaiveShuffle1 {
3+
4+
private int N;
5+
private int n, m;
6+
7+
public NaiveShuffle1(int N, int n, int m){
8+
9+
if(N <= 0)
10+
throw new IllegalArgumentException("N must be larger than 0!");
11+
12+
if(n < m)
13+
throw new IllegalArgumentException("n must be larger than or equal to m!");
14+
15+
this.N = N;
16+
this.n = n;
17+
this.m = m;
18+
}
19+
20+
public void run(){
21+
22+
int freq[] = new int[n];
23+
24+
int arr[] = new int[n];
25+
for(int i = 0 ; i < N ; i ++){
26+
27+
reset(arr);
28+
shuffle(arr);
29+
for(int j = 0 ; j < n ; j ++)
30+
freq[j] += arr[j];
31+
}
32+
33+
for(int i = 0 ; i < n ; i ++){
34+
System.out.println(i + " : " + (double)freq[i]/N);
35+
}
36+
}
37+
38+
private void reset(int[] arr){
39+
40+
for(int i = 0 ; i < m ; i ++)
41+
arr[i] = 1;
42+
43+
for(int i = m ; i < n ; i ++)
44+
arr[i] = 0;
45+
46+
return;
47+
}
48+
49+
private void shuffle(int[] arr){
50+
51+
int swapTime = n;
52+
for(int i = 0 ; i < swapTime ; i ++){
53+
int x = (int)(Math.random() * n);
54+
int y = (int)(Math.random() * n);
55+
swap(arr, x, y);
56+
}
57+
}
58+
59+
private void swap(int[] arr, int x, int y){
60+
61+
int t = arr[x];
62+
arr[x] = arr[y];
63+
arr[y] = t;
64+
}
65+
66+
public static void main(String[] args) {
67+
68+
int N = 10000000;
69+
NaiveShuffle1 exp = new NaiveShuffle1(N, 10, 5);
70+
exp.run();
71+
72+
}
73+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
public class NaiveShuffle2 {
3+
4+
private int N;
5+
private int n, m;
6+
7+
public NaiveShuffle2(int N, int n, int m){
8+
9+
if(N <= 0)
10+
throw new IllegalArgumentException("N must be larger than 0!");
11+
12+
if(n < m)
13+
throw new IllegalArgumentException("n must be larger than or equal to m!");
14+
15+
this.N = N;
16+
this.n = n;
17+
this.m = m;
18+
}
19+
20+
public void run(){
21+
22+
int freq[] = new int[n];
23+
24+
int arr[] = new int[n];
25+
for(int i = 0 ; i < N ; i ++){
26+
27+
reset(arr);
28+
shuffle(arr);
29+
for(int j = 0 ; j < n ; j ++)
30+
freq[j] += arr[j];
31+
}
32+
33+
for(int i = 0 ; i < n ; i ++){
34+
System.out.println(i + " : " + (double)freq[i]/N);
35+
}
36+
}
37+
38+
private void reset(int[] arr){
39+
40+
for(int i = 0 ; i < m ; i ++)
41+
arr[i] = 1;
42+
43+
for(int i = m ; i < n ; i ++)
44+
arr[i] = 0;
45+
46+
return;
47+
}
48+
49+
private void shuffle(int[] arr){
50+
51+
// for(int i = 0 ; i < m ; i ++){
52+
// int x = (int)(Math.random() * n);
53+
// swap(arr, i, x);
54+
// }
55+
56+
for(int i = 0 ; i < n ; i ++){
57+
int x = (int)(Math.random() * n);
58+
swap(arr, i, x);
59+
}
60+
}
61+
62+
private void swap(int[] arr, int x, int y){
63+
64+
int t = arr[x];
65+
arr[x] = arr[y];
66+
arr[y] = t;
67+
}
68+
69+
public static void main(String[] args) {
70+
71+
int N = 10000000;
72+
NaiveShuffle2 exp = new NaiveShuffle2(N, 10, 5);
73+
exp.run();
74+
75+
}
76+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
public class KnuthShuffle {
3+
4+
private int N;
5+
private int n, m;
6+
7+
public KnuthShuffle(int N, int n, int m){
8+
9+
if(N <= 0)
10+
throw new IllegalArgumentException("N must be larger than 0!");
11+
12+
if(n < m)
13+
throw new IllegalArgumentException("n must be larger than or equal to m!");
14+
15+
this.N = N;
16+
this.n = n;
17+
this.m = m;
18+
}
19+
20+
public void run(){
21+
22+
int freq[] = new int[n];
23+
24+
int arr[] = new int[n];
25+
for(int i = 0 ; i < N ; i ++){
26+
27+
reset(arr);
28+
shuffle(arr);
29+
for(int j = 0 ; j < n ; j ++)
30+
freq[j] += arr[j];
31+
}
32+
33+
for(int i = 0 ; i < n ; i ++){
34+
System.out.println(i + " : " + (double)freq[i]/N);
35+
}
36+
}
37+
38+
private void reset(int[] arr){
39+
40+
for(int i = 0 ; i < m ; i ++)
41+
arr[i] = 1;
42+
43+
for(int i = m ; i < n ; i ++)
44+
arr[i] = 0;
45+
46+
return;
47+
}
48+
49+
private void shuffle(int[] arr){
50+
51+
// 写法 1
52+
// for(int i = 0 ; i < n ; i ++){
53+
// int x = (int)(Math.random() * (n-i)) + i;
54+
// swap(arr, i, x);
55+
// }
56+
57+
// 写法 2
58+
for(int i = n - 1 ; i >= 0 ; i --){
59+
int x = (int)(Math.random() * (i+1));
60+
swap(arr, i, x);
61+
}
62+
}
63+
64+
private void swap(int[] arr, int x, int y){
65+
66+
int t = arr[x];
67+
arr[x] = arr[y];
68+
arr[y] = t;
69+
}
70+
71+
public static void main(String[] args) {
72+
73+
int N = 10000000;
74+
KnuthShuffle exp = new KnuthShuffle(N, 10, 5);
75+
exp.run();
76+
77+
}
78+
}

07-Mine-Sweeper/Chapter-06-Completed-Codes/Mine-Sweeper-Core/src/AlgoVisualizer.java renamed to 07-Mine-Sweeper/Chapter-07-Completed-Codes/Mine-Sweeper-Core/src/AlgoVisualizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ else if(SwingUtilities.isRightMouseButton(event))
8686
public static void main(String[] args) {
8787

8888
int N = 20;
89-
int M = 30;
89+
int M = 20;
9090
int mineNumber = 20;
9191

9292
AlgoVisualizer vis = new AlgoVisualizer(N, M, mineNumber);

07-Mine-Sweeper/Chapter-07.key

2.92 MB
Binary file not shown.

0 commit comments

Comments
 (0)