【程序1】
題目:古典問題:有一對兔子,從出生後第3個月起每個月都生一對兔子,小兔子長到第三個月後每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數爲多少?
//這是一個菲波拉契數列問題
publicclass lianxi01 {publicstatic void main(String[] args) {
System.out.println("第1個月的兔子對數: 1");
System.out.println("第2個月的兔子對數: 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 +"個月的兔子對數: "+f2);
}
}
}
【程序2】
題目:判斷101-200之間有多少個素數,並輸出所有素數。
程序分析:判斷素數的方法:用一個數分別去除2到sqrt(這個數),如果能被整除, 則表明此數不是素數,反之是素數。
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( "素數個數是: "+ count);
}
}
【程序3】
題目:打印出所有的"水仙花數 ",所謂 "水仙花數 "是指一個三位數,其各位數字立方和等於該數本身。例如:153是一個 "水仙花數 ",因爲153=1的三次方+5的三次方+3的三次方。
public class lianxi03 {
public static void main(String[] args) {
int b1, b2, b3;
for(int m=101; m<1000; m++) {
b3 = m / 100;
b2 = m % 100 / 10;
b1 = m % 10;
if((b3*b3*b3 + b2*b2*b2 + b1*b1*b1) == m) {
System.out.println(m+"是一個水仙花數"); }
}
}
}
【程序4】
題目:將一個正整數分解質因數。例如:輸入90,打印出90=2*3*3*5。
程序分析:對n進行分解質因數,應先找到一個最小的質數k,然後按下述步驟完成:
(1)如果這個質數恰等於n,則說明分解質因數的過程已經結束,打印出即可。
(2)如果n <> k,但n能被k整除,則應打印出k的值,並用n除以k的商,作爲新的正整數你n,重複執行第一步。
(3)如果n不能被k整除,則用k+1作爲k的值,重複執行第一步。
import java.util.*;
public class lianxi04{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print( "請鍵入一個正整數: ");
int n = s.nextInt();
int k=2;
System.out.print(n + "=");
while(k <= n) {
if(k == n){System.out.println(n);break;}
else if( n %k == 0) {System.out.print(k + "*");n = n / k; }
else k++;
}
}
}
【程序5】
題目:利用條件運算符的嵌套來完成此題:學習成績> =90分的同學用A表示,60-89分之間的用B表示,60分以下的用C表示。
importjava.util.*;public class lianxi05 {public static void main(String[] args) { int x; char grade; Scanner s = new Scanner(System.in); System.out.print( "請輸入一個成績: ");
x = s.nextInt();
grade = x >= 90 ? 'A'
: x >= 60 ? 'B'
:'C';
System.out.println("等級爲:"+grade);
}
}
【程序6】
題目:輸入兩個正整數m和n,求其最大公約數和最小公倍數。
/**在循環中,只要除數不等於0,用較大數除以較小的數,將小的一個數作爲下一輪循環的大數,取得的餘數作爲下一輪循環的較小的數,如此循環直到較小的數的值爲0,返回較大的數,此數即爲最大公約數,最小公倍數爲兩數之積除以最大公約數。* /
import java.util.*;
public class lianxi06 {
public static void main(String[] args) {
int a ,b,m;
Scanner s = new Scanner(System.in);
System.out.print( "鍵入一個整數: ");
a = s.nextInt();
System.out.print( "再鍵入一個整數: ");
b = s.nextInt();
deff cd = new deff();
m = cd.deff(a,b);
int n = a * b / m;
System.out.println("最大公約數: " + m);
System.out.println("最小公倍數: " + n);
}
}
class deff{
public int deff(int x, int y) {
int t;
if(x < y) {
t = x;
x = y;
y = t;
}
while(y != 0) {
if(x == y) return x;
else {
int k = x % y;
x = y;
y = k;
}
}
return x;
}
}
【程序7】
題目:輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數。
import java.util.*;
public class lianxi07 {
public static void main(String[] args) {
int digital = 0;
int character = 0;
int other = 0;
int blank = 0;
char[] ch = null;
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
ch = s.toCharArray();
for(int i=0; i<ch.length; i++) {
if(ch >= '0' && ch <= '9') {
digital ++;
} else if((ch >= 'a' && ch <= 'z')|| ch > 'A' && ch <= 'Z') {
character ++;
} else if(ch == ' ') {
blank ++;
} else {
other ++;
}
}
System.out.println("數字個數: " + digital);
System.out.println("英文字母個數: " + character);
System.out.println("空格個數: " + blank);
System.out.println("其他字符個數:" + other );
}
}
【程序8】
題目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數字。例如2+22+222+2222+22222(此時共有5個數相加),幾個數相加有鍵盤控制。
import java.util.*;
public class lianxi08 {
public static void main(String[] args) {
long a , b = 0, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("輸入數字a的值: ");
a = s.nextInt();
System.out.print("輸入相加的項數:");
int n = s.nextInt();
int i = 0;
while(i < n) {
b = b + a;
sum = sum + b;
a = a * 10;
++ i;
}
System.out.println(sum);
}
}
【程序9】
題目:一個數如果恰好等於它的因子之和,這個數就稱爲 "完數 "。例如6=1+2+3.編程 找出1000以內的所有完數。
public class lianxi09 {
public static void main(String[] args) {
System.out.println("1到1000的完數有: ");
for(int i=1; i<1000; i++) {
int t = 0;
for(int j=1; j<= i/2; j++) {
if(i % j == 0) {
t = t + j;
}
}
if(t == i) {
System.out.print(i +" ");
}
}
}
【程序10】
題目:一球從100米高度自由落下,每次落地後反跳回原高度的一半;再落下,求它在 第10次落地時,共經過多少米?第10次反彈多高?
public class lianxi10 {
public static void main(String[] args) {
double h = 100,s = 100;
for(int i=1; i<10; i++) {
s = s + h;
h = h / 2;
}
System.out.println("經過路程:" + s);
System.out.println("反彈高度:" + h / 2);
}
}
【程序11】
題目:有1、2、3、4四個數字,能組成多少個互不相同且無重複數字的三位數?都是多少?
public class lianxi11 {
public static void main(String[] args) {
int count = 0;
for(int x=1; x<5; x++) {
for(int y=1; y<5; y++) {
for(int z=1; z<5; z++) {
if(x != y && y != z&& x != z) {
count ++;
System.out.println(x*100 +y*10 + z );
}
}
}
}
System.out.println("共有" + count + "個三位數");
}
}
【程序12】
題目:企業發放的獎金根據利潤提成。利潤(I)低於或等於10萬元時,獎金可提10%;利潤高於10萬元,低於20萬元時,低於10萬元的部分按10%提成,高於10萬元的部分,可可提成7.5%;20萬到40萬之間時,高於20萬元的部分,可提成5%;40萬到60萬之間時高於40萬元的部分,可提成3%;60萬到100萬之間時,高於60萬元的部分,可提成1.5%,高於100萬元時,超過100萬元的部分按1%提成,從鍵盤輸入當月利潤,求應發放獎金總數?
import java.util.*;
public class lianxi12 {
public static void main(String[] args) {
double x = 0,y = 0;
System.out.print("輸入當月利潤(萬):");
Scanner s = new Scanner(System.in);
x = s.nextInt();
if(x > 0 && x <= 10) {
y = x * 0.1;
} else if(x > 10 && x <= 20) {
y = 10 * 0.1 + (x - 10) * 0.075;
} else if(x > 20 && x <= 40) {
y = 10 * 0.1 + 10 * 0.075 + (x - 20) * 0.05;
} else if(x > 40 && x <= 60) {
y = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (x - 40)* 0.03;
} else if(x > 60 && x <= 100) {
y = 20 * 0.175 + 20 * 0.05 + 20 * 0.03 + (x -60) * 0.015;
} else if(x > 100) {
y = 20 * 0.175 + 40 * 0.08 + 40 * 0.015 + (x -100) * 0.01;
}
System.out.println("應該提取的獎金是 " + y + "萬");
}
}
【程序13】
題目:一個整數,它加上100後是一個完全平方數,再加上168又是一個完全平方數,請問該數是多少?
public class lianxi13 {
public static void main(String[] args) {
for(int x =1; x<100000; x++) {
if(Math.sqrt(x+100) % 1 == 0) {
if(Math.sqrt(x+268) % 1 == 0) {
System.out.println(x + "加100是一個完全平方數,再加168又是一個完全平方數");
}
}
}
}
}
/*按題意循環應該從-100開始(整數包括正整數、負整數、零),這樣會多一個滿足條件的數-99。
但是我看到大部分人解這道題目時都把題中的“整數”理解成正整數,我也就隨大流了。*/
【程序14】
題目:輸入某年某月某日,判斷這一天是這一年的第幾天?
import java.util.*;
public class lianxi14 {
public static void main(String[] args) {
int year, month, day;
int days = 0;
int d = 0;
int e;
input fymd = new input();
do {
e = 0;
System.out.print("輸入年:");
year =fymd.input();
System.out.print("輸入月:");
month = fymd.input();
System.out.print("輸入天:");
day = fymd.input();
if (year < 0 || month < 0 || month > 12 ||day < 0 || day > 31) {
System.out.println("輸入錯誤,請重新輸入!");
e=1 ;
}
}while( e==1);
for (int i=1; i <month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
if ((year % 400 == 0) || (year % 4 == 0&& year % 100 != 0)) {
days = 29;
} else {
days = 28;
}
break;
}
d += days;
}
System.out.println(year + "-" + month +"-" + day + "是這年的第" +(d+day) + "天。");
}
}
class input{
public int input() {
int value = 0;
Scanner s = new Scanner(System.in);
value = s.nextInt();
return value;
}
}
【程序15】
題目:輸入三個整數x,y,z,請把這三個數由小到大輸出。
import java.util.*;
public class lianxi15 {
public static void main(String[] args) {
input fnc = new input();
int x=0, y=0, z=0;
System.out.print("輸入第一個數字:");
x = fnc.input();
System.out.print("輸入第二個數字:");
y = fnc.input();
System.out.print("輸入第三個數字:");
z = fnc.input();
if(x > y) {
int t = x;
x = y;
y = t;
}
if(x > z) {
int t = x;
x = z;
z = t;
}
if(y > z) {
int t = y;
y = z;
z = t;
}
System.out.println( "三個數字由小到大排列爲: "+x + " " + y + " " + z);
}
}
class input{
public int input() {
int value = 0;
Scanner s = new Scanner(System.in);
value = s.nextInt();
return value;
}
}