【程序1】
題目:古典問題:有一對兔子,從出生後第3個月起每個月都生一對兔子,小兔子長到第三個月後每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數爲多少?
//這是一個菲波拉契數列問題
public class lianxi01 {
public static 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表示。
import java.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】
題目:輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數。
package WuYang;
import java.util.*;
public class lianxi07 {
public static void main(String[] args) {
int abcCount=0;//英文字母個數
int spaceCount=0;//空格鍵個數
int numCount=0;//數字個數
int otherCount=0;//其他字符個數
Scanner scan = new Scanner(System.in);//掃描器接受控制檯的輸入信息
System.out.println("輸入一組字符");
String str=scan.nextLine();
char[] ch = str.toCharArray();
for(int i=0;i<ch.length;i++){
if(Character.isLetter(ch[i])){
//判斷是否字母
abcCount++;
}
else if(Character.isDigit(ch[i])){
//判斷是否數字
numCount++;
}
else if(Character.isSpaceChar(ch[i])){
//判斷是否空格鍵
spaceCount++;
}
else{
//以上都不是則認爲是其他字符
otherCount++;
}
}
System.out.println("字母個數:"+abcCount);
System.out.println("數字個數:"+numCount);
System.out.println("空格個數:"+spaceCount);
System.out.println("其他字符個數:"+otherCount);
}
}
【程序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++) { //如果是求和可以只要循環到i/2 如果是求素數那麼循環到math.sqrt(i)
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 = 0;
for(int i=1; i<=10; i++) {
s = s + 2*h;
h = h / 2;
}
s=s-100;
System.out.println("經過路程:" + s);
System.out.println("最後高度:" + h);
}
}
【程序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++) {//爲什麼是100000
if(Math.sqrt(x+100) % 1 == 0) {//%1 是爲了得到的整數
if(Math.sqrt(x+168) % 1 == 0) {
System.out.println(x + "加100是一個完全平方數,再加168又是一個完全平方數");
}
}
}
}
}
【程序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;
}
}