陸續收集一些簡單算法的實現

本文算法均從網絡中收集

/**
* <p>功能描述:求一個四位數,使它等於它的四個數字和的四次方</p>
*
* @param args
* @author 胡曉 <BR> [email protected] <BR>
* 時間:2010-3-17 上午07:36:34 <BR>
*/
public static void main(String[] args) {
int a, b, c, d, e;
for (int i = 1000; i < 10000; i++) {
a = i % 10;
b = i / 10 % 10;
c = i / 100 % 10;
d = i / 1000;
e = a + b + c + d;
if (e * e * e * e == i) {
System.out.println("滿足條件的數字:" + i);
}
}
}



/**
* <p>功能描述:滿足這個條件的五位數,abcdef = a^5 + b^5 + c^5 + d^5 + e^5</p>
*
* @param args
* @author 胡曉 <BR> [email protected] <BR>
* 時間:2010-3-17 上午07:46:32 <BR>
*/
public static void main(String[] args) {
int m, s, a, b, c, d, e;
// s = Integer.parseInt(args[0]);
s = 99999;
if (s >= 10000 && s <= 99999) {
System.out.println("小於該數的梅花數有:");
for (m = 10000; m < s; m++) {
a = m % 10;
b = m / 10 % 10;
c = m / 100 % 10;
d = m / 1000 % 10;
e = m / 10000;
if (m == a * a * a * a * a + b * b * b * b * b + c * c * c * c * c + d * d * d * d * d + e * e * e * e * e) {
System.out.println(m);
}
}
}
}



/**
* <p>功能描述:輸出100內的素數</p>
*
* @param args
* @author 胡曉 <BR> [email protected] <BR>
* 時間:2010-3-17 上午07:57:54 <BR>
*/
public static void main(String[] args) {
int s = 0;
int i;
for (i = 0; i <= 100; i++) {
int j;
for (j = 2; j <= i; j++) {
if (i % j == 0)
break;
}
if (i == j)
System.out.println(i);
}
}




public static void main(String[] args) {
System.out.println(isPrime(97));
}

/**
* <p>功能描述:判斷是否是素數</p>
*
* @param n
* @return
* @author 胡曉 <BR> [email protected] <BR>
* 時間:2010-3-17 上午08:02:56 <BR>
*/
public static boolean isPrime(int n) {
// filte negative, zero, one
if (1 >= n) {
return false;
}
// 2 is a prime, stop filter
if (2 == n) {
return true;
}
// filter evens
if (0 == n % 2) {
return false;
}
// go on filting...
for (int a = 3; a <= Math.sqrt(n); a += 2) {
if (0 == n % a) {
return false;
}
}
// the rest is all prime, stop filting
return true;
}



public static void main(String[] args) {
System.out.println(isPrime(97));
}

/**
* <p>功能描述:判斷是否是素數</p>
*
* @param n
* @return
* @author 胡曉 <BR> [email protected] <BR>
* 時間:2010-3-17 上午08:04:55 <BR>
*/
public static boolean isPrime(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}



public static void main(String[] args) {
System.out.println(isPrime(97));
}

/**
* <p>功能描述:判斷是否是素數</p>
*
* @param n
* @return
* @author 胡曉 <BR> [email protected] <BR>
* 時間:2010-3-17 上午08:09:48 <BR>
*/
public static boolean isPrime(int n){
int i ;
for(i=2; i <= Math.sqrt(n); i++){
if(n%i == 0 ){
break ;
}
}
return i > Math.sqrt(n);
}




/*
通常每4年裏有3個平年,1個閏年。公曆年份是4的倍數的一般是閏年,公曆年份是整百數的,必須是400的倍數纔是閏年。這是因爲地球繞太陽旋轉一週的時間,根據精確測定,約爲365天5小時48分46秒。按一年365天(平年)計算,累計4年大約少算一天;把這一天加在2月裏,這一年就是366天(閏年)。但這樣一算,每4年又多算了44分56秒,每400年就要多算3天2小時53分20秒,所以就規定了公曆年份是整百數的必須是400的倍數纔是閏年。
*/
public static void main(String[] args) {
System.out.println(isLeapYear(1997));
}

/**
* <p>功能描述:判斷是否是閏年</p>
*
* @param year
* @return
* @author 胡曉 <BR> [email protected] <BR>
* 時間:2010-3-17 上午08:16:39 <BR>
*/
public static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}



/*
約瑟夫環(出圈):是一個數學的應用問題。

已知n個人(以編號1,2,3...n分別表示)圍坐在一張圓桌周圍。從編號爲k的人開始報數,數到m的那個人出列;他的下一個人又從1開始報數,數到m的那個人又出列;依此規律重複下去,直到圓桌周圍的人全部出列。

*/





判斷一個數是不是2的N次方,這個是EE出的題,想了半天,java裏面有一個Integer.toBinaryString(int i)來轉成二進制,如果是2的N次方的話,那麼這個數字應該是首位爲1,其他位爲0。那麼該數減一,就所有首位爲0,其他位爲1,這兩個數字按位與,就會得到0。

另外0按位與-1也是0,這個要排除
1 & 0雖然不是上面的規律,但是1 & 0 也是0,所以適用該算法

所以加一個i > 0做判斷就夠了

public static boolean testIndicial(int i) {
return i > 0 && (i & i - 1) == 0;
}





------------to be continued

兔子,迷宮,漢諾塔,出圈,簡單的加解密,SSL
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章