?? extendmath.java
字號:
package util;
/**
* Created by IntelliJ IDEA.
* User: robai
* Date: 2007-2-26
* Time: 15:15:12
* To change this template use File | Settings | File Templates.
*/
public class ExtendMath {
/* 兩個數的最小公倍數 */
public int fn(int m, int n) {
int r = 0;
for (r = m; 0 != r % n; r += m) ;
return r;
}
/* 兩個數的最大公約數 */
public int gcd(int a, int b) {
if (a < b) {
int temp = a;
a = b;
b = temp;
}
if (0 == b)//the base case
return a;
if (a % 2 == 0 && b % 2 == 0)//a and b are even
return 2 * gcd(a / 2, b / 2);
if (a % 2 == 0)// only a is even
return gcd(a / 2, b);
if (b % 2 == 0)// only b is even
return gcd(a, b / 2);
return gcd((a + b) / 2, (a - b) / 2);// a and b are odd
}
public int getGreatestCommonDivisor(int long1, int long2) {
int bOne;
int sOne;
if (long1 < long2) {
bOne = long1;
sOne = long2;
} else {
bOne = long2;
sOne = long1;
}
if (bOne % sOne == 0) {
return sOne;
}
int t = sOne / 2;
for (int i = t; i > 1; i--) {
if (bOne % i == 0 && sOne % i == 0) {
return i;
}
}
return 1;
}
public static void main(String[] argsp) {
ExtendMath math = new ExtendMath();
System.out.println("64,4最大公約數:" + math.gcd(64, 4));
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -