Skip to content

Commit 0beb98c

Browse files
committed
Add RandomSeq
1 parent 178222e commit 0beb98c

File tree

5 files changed

+254
-1
lines changed

5 files changed

+254
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ lib
44
*.bs.js
55
.DS_Store
66
*.class
7+
.idea

java/README.md

+229
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,232 @@ public static int gcd(int p, int q) {
1717
return gcd(q, r);
1818
}
1919
```
20+
21+
### 1.1 基础编程模型
22+
23+
#### 1.1.1 二分查找
24+
25+
二分查找是一种在有序数组中查找某一特定元素的搜索算法。搜索过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜索过程结束;如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一样从中间元素开始比较。如果在某一步骤数组为空,则代表找不到。
26+
27+
```java
28+
public class BinarySearch {
29+
30+
public static void main(String[] args) {
31+
int[] whitelist = In.readInts(args[0]);
32+
33+
Arrays.sort(whitelist);
34+
35+
while (!StdIn.isEmpty()) {
36+
int key = StdIn.readInt();
37+
if (rank(key, whitelist) == -1) StdOut.println(key);
38+
}
39+
}
40+
41+
public static int rank(int key, int[] a) {
42+
int lo = 0;
43+
int hi = a.length - 1;
44+
while (lo <= hi) {
45+
// Key is in a[lo..hi] or not present.
46+
int mid = lo + (hi - lo) / 2;
47+
if (key < a[mid]) {
48+
hi = mid - 1;
49+
} else if (key > a[mid]) {
50+
lo = mid + 1;
51+
} else {
52+
return mid;
53+
}
54+
}
55+
return -1;
56+
}
57+
}
58+
```
59+
60+
#### 1.1.2 原始数据类型与表达式
61+
62+
需要注意的是类型转换,例如:
63+
64+
```java
65+
(int)3.7 == 3
66+
(double)3 == 3.0
67+
```
68+
69+
下面是Rust语言中的类型转换:
70+
71+
```rust
72+
fn main() {
73+
let x = 3.0 as i32;
74+
println!("{}", x); // 3
75+
}
76+
```
77+
78+
#### 1.1.5 数组
79+
80+
数组的初始化
81+
82+
```java
83+
double a[] = new double[N];
84+
85+
int[] a = {1, 1, 3, 4, 6, 9};
86+
```
87+
88+
一些典型的数组处理的代码片段:
89+
90+
```java
91+
// 计算数组元素的平均值
92+
public static double average(double[] a) {
93+
int N = a.length;
94+
double sum = 0.0;
95+
for (int i = 0; i < N; i++) {
96+
sum += a[i];
97+
}
98+
return sum / N;
99+
}
100+
101+
// 复制数组
102+
public static double[] copy(double[] a) {
103+
int N = a.length;
104+
double[] b = new double[N];
105+
for (int i = 0; i < N; i++) {
106+
b[i] = a[i];
107+
}
108+
return b;
109+
}
110+
111+
// 颠倒数组元素的顺序
112+
public static void reverse(double[] a) {
113+
int N = a.length;
114+
for (int i = 0; i < N / 2; i++) {
115+
double temp = a[i];
116+
a[i] = a[N - 1 - i];
117+
a[N - 1 - i] = temp;
118+
}
119+
}
120+
121+
// 找出数组中最大的元素
122+
public static double max(double[] a) {
123+
int N = a.length;
124+
double max = a[0];
125+
for (int i = 1; i < N; i++) {
126+
if (a[i] > max) {
127+
max = a[i];
128+
}
129+
}
130+
return max;
131+
}
132+
133+
// 矩阵相乘
134+
// a[][] * b[][] = c[][]
135+
public static double[][] matrixMult(double[][] a, double[][] b) {
136+
int N = a.length;
137+
double[][] c = new double[N][N];
138+
for (int i = 0; i < N; i++) {
139+
for (int j = 0; j < N; j++) {
140+
for (int k = 0; k < N; k++) {
141+
c[i][j] += a[i][k] * b[k][j];
142+
}
143+
}
144+
}
145+
return c;
146+
}
147+
```
148+
149+
#### 1.5.4 java中的数组别名
150+
151+
```java
152+
int[] a = new int[10];
153+
154+
a[i] = 1234;
155+
156+
int[] b = a;
157+
158+
b[i] = 5678; // a[i] == 5678
159+
```
160+
161+
#### 1.1.6 静态方法
162+
163+
静态方法是不需要创建对象就可以调用的方法。静态方法在声明时使用关键字static,静态方法不能访问非静态的成员变量和方法。
164+
165+
```java
166+
public static double sqrt(double c) {
167+
if (c < 0) return Double.NaN;
168+
double err = 1e-15;
169+
double t = c;
170+
while (Math.abs(t - c / t) > err * t) {
171+
t = (c / t + t) / 2.0;
172+
}
173+
return t;
174+
}
175+
```
176+
177+
典型的静态方法的实现
178+
179+
```java
180+
// 计算一个整数的绝对值
181+
public static int abs(int x) {
182+
if (x < 0) return -x;
183+
else return x;
184+
}
185+
186+
// 计算一个浮点数的绝对值
187+
public static double abs(double x) {
188+
if (x < 0.0) return -x;
189+
else return x;
190+
}
191+
192+
// 判断一个数是否是素数
193+
public static boolean isPrime(int N) {
194+
if (N < 2) return false;
195+
for (int i = 2; i * i <= N; i++) {
196+
if (N % i == 0) return false;
197+
}
198+
return true;
199+
}
200+
201+
// 计算平方根
202+
public static double sqrt(double c) {
203+
if (c < 0) return Double.NaN;
204+
double err = 1e-15;
205+
double t = c;
206+
while (Math.abs(t - c / t) > err * t) {
207+
t = (c / t + t) / 2.0;
208+
}
209+
return t;
210+
}
211+
212+
// 计算直角三角形的斜边
213+
public static double hypotenuse(double a, double b) {
214+
return Math.sqrt(a * a + b * b);
215+
}
216+
217+
// 计算调和级数
218+
public static double H(int N) {
219+
double sum = 0.0;
220+
for (int i = 1; i <= N; i++) {
221+
sum += 1.0 / i;
222+
}
223+
return sum;
224+
}
225+
```
226+
227+
#### 1.1.7 递归
228+
229+
递归是一种解决问题的方法,它把一个问题分解为越来越小的子问题。递归的三个重要特征:
230+
231+
1. 递归调用是方法调用自身。
232+
2. 递归调用必须有一个最简单的情况作为方法的返回条件。
233+
3. 递归调用的过程中,每次调用之间的参数都应有所变化,以缩小问题的规模。
234+
235+
```java
236+
public static int rank(int key, int[] a) {
237+
return rank(key, a, 0, a.length - 1);
238+
}
239+
240+
public static int rank(int key, int[] a, int lo, int hi) {
241+
// 如果key存在于a[]中,它的索引不会小于lo且不会大于hi
242+
if (lo > hi) return -1;
243+
int mid = lo + (hi - lo) / 2;
244+
if (key < a[mid]) return rank(key, a, lo, mid - 1);
245+
else if (key > a[mid]) return rank(key, a, mid + 1, hi);
246+
else return mid;
247+
}
248+
```

java/src/main/java/com/mycompany/app/App.java

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.mycompany.app;
22

33
import com.mycompany.binarysearch.BinarySearch;
4+
import java.lang.Math;
45

56
/**
67
* Hello world!
@@ -15,6 +16,10 @@ public static void main(String[] args) {
1516
// test rank
1617
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
1718
System.out.println(BinarySearch.rank(5, a));
19+
20+
System.out.println(Math.sin(Math.PI / 2));
21+
System.out.println(Math.cos(Math.PI / 2));
22+
System.out.println(Math.tan(Math.PI / 2));
1823
}
1924

2025
/// gcd algorithm
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.mycompany.app;
2+
3+
import com.mycompany.standrard.StdOut;
4+
import com.mycompany.standrard.StdRandom;
5+
6+
public class RandomSeq {
7+
public static void main(String[] args) {
8+
int N = Integer.parseInt(args[0]);
9+
double lo = Double.parseDouble(args[1]);
10+
double hi = Double.parseDouble(args[2]);
11+
12+
for (int i = 1; i <= N; i++) {
13+
double x = StdRandom.uniform(lo, hi);
14+
StdOut.printf("%.2f\n", x);
15+
}
16+
}
17+
}
18+

java/src/main/java/com/mycompany/binarysearch/BinarySearch.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.Arrays;
77

88
/**
9-
* Hello world!
9+
* BinarySearch
1010
*
1111
*/
1212
public class BinarySearch {

0 commit comments

Comments
 (0)