課程詳情 學校簡介 學校地址 網(wǎng)上報名
關鍵詞:西安西科軟件實訓中心 西安IT培訓學校 西安java軟件工程師培訓
西安java培訓--軟件工程師培訓/西科軟件培訓學校
這篇文章主要介紹了Java求素數(shù)和最大公約數(shù)的簡單代碼示例,其中作者創(chuàng)建的Fraction類可以用來進行各種分數(shù)運算,需要的朋友可以參考下
Java小例子:求素數(shù)
素數(shù)(質(zhì)數(shù))指的是不能被分解的數(shù),除了 1 和它本身之外就沒有其它數(shù)能夠整除。這里是一個小例子,說明如何求取十萬以內(nèi)的所有素數(shù)。
素數(shù)的分布沒有規(guī)律可言,所以要檢驗一個數(shù)是不是素數(shù),就必須將它同所有小于它的數(shù)作除法。不過有一個簡便的方法,就是不需要檢驗所有小于它的數(shù),而只要檢驗所有小于它的素數(shù)。如果所有小于它的素數(shù)都不能將其整除,那么它就是素數(shù)。
|
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
|
public class Primes {
public static void main(String[] args) {
// 求素數(shù)
Listprimes = getPrimes(100000);
// 輸出結果
for (int i = 0; i < primes.size(); i++) {
Integer prime = primes.get(i);
System.out.printf("%8d", prime);
if (i % 10 == 9) {
System.out.println();
}
}
}
/**
* 求 n 以內(nèi)的所有素數(shù)
*
* @param n 范圍
*
* @return n 以內(nèi)的所有素數(shù)
*/
private static ListgetPrimes(int n) {
Listresult = new ArrayList();
result.add(2);
for (int i = 3; i <= n; i += 2) {
if (!divisible(i, result)) {
result.add(i);
}
}
return result;
}
/**
* 判斷 n 是否能被整除
*
* @param n 要判斷的數(shù)字
* @param primes 包含素數(shù)的列表
*
* @return 如果 n 能被 primes 中任何一個整除,則返回 true。
*/
private static boolean divisible(int n, Listprimes) {
for (Integer prime : primes) {
if (n % prime == 0) {
return true;
}
}
return false;
}
}
|
Java小例子:模擬分數(shù)的類 Fraction
這里是一個模擬分數(shù)運算的例子:Fraction 類。分數(shù)運算完后要用最大公約數(shù)除分子分母。所以這里也有個用輾轉(zhuǎn)相除法求最大公約數(shù)的例子。另外在構造 Fraction 對象時如果分母為零將會拋出異常,這也是必要的檢查。
|
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
public class FractionTest {
public static void main(String[] args) {
Fraction a = new Fraction(7, 32);
Fraction b = new Fraction(13, 32);
System.out.println(a + " + " + b + " = " + a.add(b) + "(" + a.add(b).doubleValue() + ")");
System.out.println(a + " - " + b + " = " + a.minus(b) + "(" + a.minus(b).doubleValue() + ")");
System.out.println(a + " * " + b + " = " + a.multiply(b) + "(" + a.multiply(b).doubleValue() + ")");
System.out.println(a + " / " + b + " = " + a.devide(b) + "(" + a.devide(b).doubleValue() + ")");
}
}
// 分數(shù)
class Fraction {
private int numerator; // 分子
private int denominator; // 分母
Fraction(int numerator, int denominator) {
if (denominator == 0) {
throw new IllegalArgumentException("分母不能為 0");
}
this.numerator = numerator;
this.denominator = denominator;
shrink();
}
Fraction() {
this(0, 1);
}
public int getNumerator() {
return numerator;
}
public void setNumerator(int numerator) {
this.numerator = numerator;
}
public int getDenominator() {
return denominator;
}
public void setDenominator(int denominator) {
this.denominator = denominator;
}
// 分子分母同除以最大公約數(shù)
private Fraction shrink() {
int maxCommonDivisor = getMaxCommonDivisor(this.denominator, this.numerator);
this.numerator /= maxCommonDivisor;
this.denominator /= maxCommonDivisor;
return this;
}
// 輾轉(zhuǎn)相除法求最大公約數(shù)
private int getMaxCommonDivisor(int a, int b) {
int mod = a % b;
if (mod == 0) {
return b;
} else {
return getMaxCommonDivisor(b, mod);
}
}
// 分數(shù)加法
public Fraction add(Fraction that) {
return new Fraction(this.numerator * that.denominator + this.denominator * that.numerator,
this.denominator * that.denominator);
}
// 分數(shù)減法
public Fraction minus(Fraction that) {
return new Fraction(this.numerator * that.denominator - this.denominator * that.numerator,
this.denominator * that.denominator);
}
// 分數(shù)乘法
public Fraction multiply(Fraction that) {
return new Fraction(this.numerator * that.numerator,
this.denominator * that.denominator);
}
// 分數(shù)除法
public Fraction devide(Fraction that) {
return new Fraction(this.numerator * that.denominator,
this.denominator * that.numerator);
}
public double doubleValue() {
return (double) numerator / denominator;
}
@Override
public String toString() {
return String.format("{%d/%d}", this.numerator, this.denominator);
}
}
|
運行輸出:
|
1
2
3
4
|
{7/32} + {13/32} = {5/8}(0.625)
{7/32} - {13/32} = {-3/16}(-0.1875)
{7/32} * {13/32} = {91/1024}(0.0888671875)
{7/32} / {13/32} = {7/13}(0.5384615384615384)
|
西安java培訓--軟件工程師培訓/西科軟件培訓學校
西科軟件(實訓中心)是西安科技大學高新學院培養(yǎng)高素質(zhì)移動互聯(lián)網(wǎng)實用型人才的專業(yè)IT職業(yè)教育培訓機構。是國家IT精英人才孵化基地,西安軟件園移動互聯(lián)網(wǎng)聯(lián)盟指定的IT人才輸送基地。 西科軟件與西安科技大學高新學院同屬西科集團,實訓基地設在西安科技大學高新學院 ,在資源共享上占有絕對優(yōu)勢,學院坐落在西部大學城,占地800余畝,60%以上綠化,是一所現(xiàn)代化園林式院校,環(huán)境優(yōu)美。 西科軟件件實訓中心“因材施教”,根據(jù)學員的不同類型進行有針對性的教授,實行小班“一人一機”教學,2:8的授課方式,20%理論知識學習時間,80%的上機操作時間,及時將所學專業(yè)付諸于實踐操作中,能更好的鞏固了所學專業(yè),真正的將專業(yè)知識轉(zhuǎn)化為專業(yè)技術,完全秉承了“技術實訓”的教育原則。 扎實的動手操作能力,豐富的項目實訓經(jīng)驗,提前體驗了軟件企業(yè)項目操作流程,使學生與企業(yè)可以無縫接軌,自然過渡到企業(yè)項目開發(fā)中去。同時西科軟件為企業(yè)按需定制、培養(yǎng)各類IT人才,及時、長期地提供企業(yè)所急需的高素質(zhì)軟件開發(fā)及產(chǎn)品運營人才,協(xié)助解決企業(yè)人才儲備不足的后顧之憂,成為學生和企業(yè)之間無縫鏈接的橋梁。西科軟件具備優(yōu)秀的課程研發(fā)團隊、穩(wěn)定的講師團隊和完善的就業(yè)保障體系。 實戰(zhàn)為主的授課模式、企業(yè)委派的一線師資及精良的教學硬件設施,多方位保障學員的學習和教學質(zhì)量;與數(shù)十家大中型軟件企業(yè)簽訂的“軟件人才輸送計劃”,充分保障了學員的就業(yè)空間。真正做到了以就業(yè)為目的,以專業(yè)為保障的服務宗旨。 “一站式”就業(yè)模式,更加直接有效地解決了軟件行業(yè)的人才短缺問題,并且為廣大有志從事軟件開發(fā)行業(yè)相關人員提供了良好的機會與平臺。
學校名稱:西安西科軟件實訓中心
固定電話:
授課地址:海星城市廣場 預約參觀