題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數(shù)為多少?
//這是一個菲波拉契數(shù)列問題
public class lianxi01 {
public static void main(String[] args) {
System.out.println("第1個月的兔子對數(shù): 1");
System.out.println("第2個月的兔子對數(shù): 1");
int f1 = 1, F2 = 1, f, M=24;
for(int i=3; i<=M; i++) {
f = F2;
F2 = f1 + F2;
f1 = f;
System.out.println("第" + i +"個月的兔子對數(shù): "+F2);
}
}
}
【程序2】
題目:判斷101-200之間有多少個素數(shù),并輸出所有素數(shù)。
程序分析:判斷素數(shù)的方法:用一個數(shù)分別去除2到sqrt(這個數(shù)),如果能被整除, 則表明此數(shù)不是素數(shù),反之是素數(shù)。
public class lianxi02 {
public static void main(String[] args) {
int count = 0;
for(int i=101; i<200; i+=2) {
boolean b = false;
for(int j=2; j<=Math.sqrt(i); j++)
{
if(i % j == 0) { b = false; break; }
else { b = true; }
}
if(b == true) {count ++;System.out.println(i );}
}
System.out.println( "素數(shù)個數(shù)是: " + count);
}
}
【程序3】
題目:打印出所有的 "水仙花數(shù) ",所謂 "水仙花數(shù) "是指一個三位數(shù),其各位數(shù)字立方和等于該數(shù)本身。例如:153是一個 "水仙花數(shù) ",因為153=1的三次方+5的三次方+3的三次方。
public class lianxi03 {
public static void main(String[] args) {
int b1, b2, b3;
標簽:
java
編程
上傳時間:
2017-12-24
上傳用戶:Ariza