Skip to content

Commit b56d252

Browse files
committed
add java
1 parent f56680c commit b56d252

File tree

7 files changed

+182
-2
lines changed

7 files changed

+182
-2
lines changed

.DS_Store

0 Bytes
Binary file not shown.

.gitignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/target
1+
target
22
node_modules
33
lib
4-
*.bs.js
4+
*.bs.js
5+
.DS_Store

java/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 算法
2+
3+
## chapter1: basic
4+
5+
算法的定义:一种有限、确定、有效的并适合用计算机程序来实现的解决问题的方法。
6+
7+
算法是计算机科学的基础,是这个领域的核心。算法是一种解决问题的方法,是一种思维活动,是一种设计和分析的技术。
8+
9+
欧几里得算法
10+
11+
自然语言描述: 计算两个非负整数p和q的最大公约数:若q是0,则最大公约数为p。否则,将p除以q得到余数r,p和q的最大公约数即为q和r的最大公约数。
12+
13+
```java
14+
public static int gcd(int p, int q) {
15+
if (q == 0) return p;
16+
int r = p % q;
17+
return gcd(q, r);
18+
}
19+
```

java/pom.xml

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.mycompany.app</groupId>
8+
<artifactId>java</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<name>java</name>
12+
<!-- FIXME change it to the project's website -->
13+
<url>http://www.example.com</url>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<maven.compiler.source>1.7</maven.compiler.source>
18+
<maven.compiler.target>1.7</maven.compiler.target>
19+
<maven.compiler.release>11</maven.compiler.release>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>junit</groupId>
25+
<artifactId>junit</artifactId>
26+
<version>4.11</version>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
33+
<plugins>
34+
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
35+
<plugin>
36+
<artifactId>maven-clean-plugin</artifactId>
37+
<version>3.1.0</version>
38+
</plugin>
39+
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
40+
<plugin>
41+
<artifactId>maven-resources-plugin</artifactId>
42+
<version>3.0.2</version>
43+
</plugin>
44+
<plugin>
45+
<artifactId>maven-compiler-plugin</artifactId>
46+
<version>3.8.1</version>
47+
</plugin>
48+
<plugin>
49+
<artifactId>maven-surefire-plugin</artifactId>
50+
<version>2.22.1</version>
51+
</plugin>
52+
<plugin>
53+
<artifactId>maven-jar-plugin</artifactId>
54+
<version>3.0.2</version>
55+
</plugin>
56+
<plugin>
57+
<artifactId>maven-install-plugin</artifactId>
58+
<version>2.5.2</version>
59+
</plugin>
60+
<plugin>
61+
<artifactId>maven-deploy-plugin</artifactId>
62+
<version>2.8.2</version>
63+
</plugin>
64+
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
65+
<plugin>
66+
<artifactId>maven-site-plugin</artifactId>
67+
<version>3.7.1</version>
68+
</plugin>
69+
<plugin>
70+
<artifactId>maven-project-info-reports-plugin</artifactId>
71+
<version>3.0.0</version>
72+
</plugin>
73+
</plugins>
74+
</pluginManagement>
75+
</build>
76+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.mycompany.app;
2+
3+
import com.mycompany.binarysearch.BinarySearch;
4+
5+
/**
6+
* Hello world!
7+
*
8+
*/
9+
public class App {
10+
11+
public static void main(String[] args) {
12+
System.out.println("Hello Davirain!");
13+
// test gcd
14+
System.out.println(gcd(10, 5));
15+
// test rank
16+
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
17+
System.out.println(BinarySearch.rank(5, a));
18+
}
19+
20+
/// gcd algorithm
21+
public static int gcd(int p, int q) {
22+
if (q == 0) {
23+
return p;
24+
} else {
25+
return gcd(q, p % q);
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.mycompany.binarysearch;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Hello world!
7+
*
8+
*/
9+
public class BinarySearch {
10+
11+
/// gcd algorithm
12+
public static int gcd(int p, int q) {
13+
if (q == 0) {
14+
return p;
15+
} else {
16+
return gcd(q, p % q);
17+
}
18+
}
19+
20+
public static int rank(int key, int[] a) {
21+
int lo = 0;
22+
int hi = a.length - 1;
23+
while (lo <= hi) {
24+
// Key is in a[lo..hi] or not present.
25+
int mid = lo + (hi - lo) / 2;
26+
if (key < a[mid]) {
27+
hi = mid - 1;
28+
} else if (key > a[mid]) {
29+
lo = mid + 1;
30+
} else {
31+
return mid;
32+
}
33+
}
34+
return -1;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.mycompany.app;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.Test;
6+
7+
/**
8+
* Unit test for simple App.
9+
*/
10+
public class AppTest
11+
{
12+
/**
13+
* Rigorous Test :-)
14+
*/
15+
@Test
16+
public void shouldAnswerWithTrue()
17+
{
18+
assertTrue( true );
19+
}
20+
}

0 commit comments

Comments
 (0)