JAVA經典算法50題

【程序1】?? 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數為多少?

1.程序分析:兔子的規律為數列1,1,2,3,5,8,13,21....
具體分析如下:
f(1) = 1(第1個月有一對兔子)
f(2) = 1(第2個月還是一對兔子)
f(3) = 2(原來有一對兔子,第3個開始,每個月生一對兔子)
f(4) = 3(原來有兩對兔子,有一對可以生育)
f(5) = 5(原來有3對兔子,第3個月出生的那對兔子也可以生育了,那么現在有兩對兔子可以生育)
f(6) = 8(原來有5對兔子,第4個月出生的那對兔子也可以生育了,那么現在有3對兔子可以生育)
..............

由以上可以看出,第n個月兔子的對數為
f(n) = f(n - 1) + f(n - 2);
f(n-1)是上個月的兔子數量,是原來有的。
f(n-2)是可以生育的兔子數,即多出來的數量。第n-2個月開始后的第3個月是第n個月,此時第n-2個月時的兔子都可以生育了。

public class Demo01 {
? ? public static void main(String args[]) {
????????for (int i = 1; i <= 20; i++) {
????????????System.out.println(f(i));
????????}
????}

?? ?public static int f(int x) {
?????????if (x == 1 || x == 2) {
?????????????return 1;
????????} else {
? ??????????return f(x - 1) + f(x - 2);
????????}
? ? }
}

public class Demo01 {
????public static void main(String args[]) {
????????math mymath = new math();
? ? ? ? for (int i = 1; i <= 20; i++) {
????????????System.out.println(mymath.f(i));
????????}
????}
}

class math {
????public int f(int x) {
????????if (x == 1 || x == 2) {
????????????return 1;
????????} else {
????????????return f(x - 1) + f(x - 2);
????????}
????}
}


【程序2】?? 題目:判斷101-200之間有多少個素數,并輸出所有素數。

1.程序分析:判斷素數的方法:用一個數分別去除2到sqrt(這個數),如果能被整除,則表明此數不是素數,反之是素數。

public class Demo02 {
????public static void main(String[] args) {
????????for(int i = 2;i <= 200;i++) {
????????????boolean flag = true;
????????????for(int j = 2;j < i; j++) {
????????????????if(i % j == 0) {
????????????????????flag=false;
????????????????????break;
????????????????}
????????????}
????????????if(flag == true) {
????????????????System.out.print(" "+i);
????????????}
????????}
????}
}


【程序3】?? 題目:打印出所有的 水仙花數 ,所謂 水仙花數 是指一個三位數,其各位數字立方和等于該數本身。例如:153是一個 水仙花數 ,因為153=1的三次方+5的三次方+3的三次方。

1.程序分析:利用for循環控制100-999個數,每個數分解出個位,十位,百位。
public class Demo03 {
????public static void main(String args[]) {
????????math mymath = new math();
????????for (int i = 100; i <= 999; i++) {
????????????if (mymath.shuixianhua(i) == true) {
????????????????System.out.println(i);
????????????}
????????}
????}
}

class math {
????public boolean shuixianhua(int x) {
????????int i = 0, j = 0, k = 0;
????????i = x / 100;
????????j = (x % 100) / 10;
????????k = x % 10;
????????if (x == i * i * i + j * j * j + k * k * k) {
????????????return true;
????????}?else {
????? ? ? ?return false;
????????}
????}
}


【程序4】?? 題目:將一個正整數分解質因數。例如:輸入90,打印出90=2*3*3*5。?

1.程序分析:對n進行分解質因數,應先找到一個最小的質數i,然后按下述步驟完成:
(1)如果這個質數恰等于n,則說明分解質因數的過程已經結束,打印出即可。
(2)如果n > i,但n能被i整除,則應打印出i的值,并用n除以i的商,作為新的正整數你,重復執行第一步。
(3)如果n不能被i整除,則用i+1作為i的值,重復執行第一步。

import java.util.Scanner;
public class Demo04 {
????public Demo04() {
????????super();
????}

????public void fenjie(int n) {
????????for (int i = 2; i <= n; i++) {
????????????if (n % i == 0) {
????????????????System.out.print(i);
????????????????if (n != i) {
????????????????????System.out.print("*");
????????????????}
????????????????fenjie(n/i);
????????????}
????????}
????????System.exit(0); // 退出程序
????}

?? ?public static void main(String[] args) {
????????Scanner in = new Scanner(System.in);
????????System.out.println("請輸入N的值:");
????????int N = in.nextInt();
????????System.out.print( "分解質因數:" + N +"=");
????????new Demo04().fenjie(N);
????}
}


【程序5】?? 題目:利用條件運算符的嵌套來完成此題:學習成績=90分的同學用A表示,60-89分之間的用B表示,60分以下的用C表示。

1.程序分析:(a>b)?a:b這是條件運算符的基本例子。

import java.util.Scanner;
public class Demo05 {
????public static void main(String[] args) {
????????System.out.println("請輸入N的值:");
????????Scanner in = new Scanner(System.in);
????????int N = in.nextInt();
????????System.out.println(N >= 90 ?"A": (N >= 60 ? "B":"C"));
????}
}


【程序6】?? 題目:輸入兩個正整數m和n,求其最大公約數和最小公倍數。

1.程序分析:利用輾除法。

import java.util.Scanner;
public class Demo06 {
????public static void main(String[] args) {
????????int a,b,m,n;
????????Scanner in = new Scanner(System.in);
????????System.out.println("請輸入一個正整數:");
????????a = in.nextInt();
????????System.out.println("再輸入一個正整數:");
????????b = in.nextInt();
????????commonDivisor use = new commonDivisor();
????????m = use.commonDivisor(a,b);
????????n = a*b/m;
????????System.out.println("最大公約數:"+m);
????????System.out.println("最小公倍數:"+n);
????}
}

class commonDivisor {
????public int commonDivisor(int x,int y) {
????????if(x < y) {
????????????int 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】?? 題目:輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數。

1.程序分析:利用for循環語句,if條件語句。

import java.util.Scanner;
public class Demo07 {
????public static void main(String[] args) {
????????System.out.println("請輸入一個字符串;");
????????Scanner in = new Scanner(System.in);
????????String str = in.nextLine();
????????char[] ch = str.toCharArray();
????????count use = new count();
????????use.count(ch);
????}
}

class count{
????int digital,character,blank,other;
????public void count(char[] arr) {
????????for(int i = 0; i < arr.length(); i++) {
????????????if (arr[i] >= '0' && arr[i] <= '9') {
????????????????digital++;
????????????} else if ((arr[i] >= 'a' && arr[i] <= 'z') || (arr[i] >= 'A' && arr[i] <= 'Z')) {
????????????????character++;
????????????} else if (arr[i] == ' ') {
????????????????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個數相加),幾個數相加有鍵盤控制。

1.程序分析:關鍵是計算出每一項的值。

import java.util.Scanner;
public class Demo08 {
????public static void main(String[] args) {
????????Scanner in = new Scanner(System.in);
????????System.out.println(請輸入a的值);
????????int a = in.nextInt();
????????System.out.println(請輸入n個數);
????????int n = in.nextInt();
? ? ? ? int s = 0,t=0;
????????for (int i = 1; i <= n; i++) {
????????????t += a;
????????????a = a*10;
????????????s += t;
????????}
????????System.out.println(s);
????}
}


【程序9】?? 題目:一個數如果恰好等于它的因子之和,這個數就稱為"完數"。例如6=1+2+3。編程找出1000以內的所有完數。

public class Demo09 {
????public static void main(String[] args) {
????????int s;
????????for (int i = 1; i <= 1000; i++) {
????????????s = 0;
????????????for (int j = 1; j < i; j++) {
????????????????if (i % j == 0) {
????????????????????s = s + j;
????????????????}
????????????}
????????????if (s == i) ?{
????????????????System.out.print(i + " " );
????????????}
????????}
????????System.out.println();
????}
}


【程序10】 題目:一球從100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地時,共經過多少米?第10次反彈多高?

public class Demo10 {
????public static void main(String[] args) {
????????double s = 0;
????????double h = 100;
????????for (int i = 1; i <= 10; i++) {
????????????s += h;
????????????h = h/2;
????????????s += h;
????????}
????????System.out.println("經過路程:"+s);
????????System.out.println("反彈高度:"+h);
????}
}


【程序11】?? 題目:有1、2、3、4個數字,能組成多少個互不相同且無重復數字的三位數?都是多少?

1.程序分析:可填在百位、十位、個位的數字都是1、2、3、4。組成所有的排列后再去掉不滿足條件的排列。? ?

public class Demo11 {
????public static void main(String[] args) {
????????int count = 0;
????????for (int i = 1; i <= 4; i++) {
????????????for (int j = 1; j <= 4; j++) {
????????????????for (int k = 1; k <= 4; k++) {
????????????????????if (i != j && j != k && i != k) {
????????????????????????count += 1;
????????????????????????System.out.println(i * 100 + j * 10 + k);
????????????????????}
????????????????}
????????????}
????????}
? ? ? ? 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%提成,從鍵盤輸入當月利潤lirun,求應發放獎金總數sum?

1.程序分析:請利用數軸來分界,定位。注意定義時需把獎金定義成長整型。

import java.util.Scanner;
public class Demo12 {
????public static void main(String[] args) {
????????double sum;
????????System.out.println("輸入當月利潤:(萬元)");
????????Scanner in = new Scanner(System.in);
????????double lirun = in.nextDouble();
????????if (lirun <= 10) {
????????????sum = lirun * 0.1;
????????} else if (lirun <= 20) {
????????????sum = 10*0.1 + (lirun - 10) * 0.075;
????????} else if (lirun <= 40) {
????????????sum = 10*0.1 + 10*0.075 + (lirun - 20) * 0.05;
????????} else if (lirun <= 60) {
????????????sum = 10*0.1 + 10*0.075 + 10*0.05 + (lirun - 40) * 0.03;
????????} else if (lirun <= 100) {
????????????sum = 10*0.1 + 10*0.075 + 10*0.05 + 10*0.03 + (lirun - 60) * 0.015;
????????} else {
????????????sum = 10*0.1 + 10*0.075 + 10*0.05 + 10*0.03 + 10*0.015 + (lirun - 100) * 0.01;
????????}
????????System.out.println("應發的獎金是:"+sum+"(萬元)");
????}
}


【程序13】?? 題目:一個整數,它加上100后是一個完全平方數,加上168又是一個完全平方數,請問該數是多少?

1.程序分析:在10萬以內判斷,先將該數加上100后再開方,再將該數加上168后再開方,如果開方后的結果滿足如下條件,即是結果。請看具體分析:

public class Demo13 {
????public static void main(String[] args) {
????????for(int x = 1 ; x < 100000 ; x ++) {
? ? ? ? ? ? int value1 = Math.sqrt(x + 100);
? ? ? ? ? ? int value2 = Math.sqrt(x + 100 + 168)
? ? ? ? ? ? if (value1 * value1 == x && value2 * value2 == x) {
????????????????System.out.println(x+"加上100后是一個完全平方數,加上168又是一個完全平方數");
????????????}
????????}
????}
}


【程序14】 題目:輸入某年某月某日,判斷這一天是這一年的第幾天?

1.程序分析:以3月5日為例,應該先把前兩個月的加起來,然后再加上5天即本月的第幾天,特殊情況,閏年且輸入月份大于3時需考慮多加一天。

import java.util.Calendar;
import java.util.Scanner;
public class Demo14 {
????public static void main(String[] args) {
????????System.out.println("請輸入年,月,日:");
????????Scanner in = new Scanner(System.in);
????????int year = in.nextInt();
????????int month = in.nextInt();
????????int day = in.nextInt();
????????Calendar cal = Calendar.getInstance();
????????cal.set(year, month - 1, day);
????????int sum = cal.get(Calendar.DAY_OF_YEAR);
????????System.out.println("這一天是這一年的第" + sum +"天");
????}
}

import java.util.*;
public class Demo14 {
????public static void main(String[] args) {
????????int year,month,day,sum = 0;
????????Scanner in = new Scanner(System.in);
????????System.out.println("輸入年:");
????????year = in.nextInt();
????????System.out.println("輸入月:");
????????month = in.nextInt();
????????System.out.println("輸入日:");
????????day = in.nextInt();
????????switch(month) {
????????????case 1:
????????????????sum=0;
????????????????break;
????????????case 2:
????????????????sum=31;
? ? ? ? ? ? ? ? break;
????????????case 3:
????????????????sum=59;
????????????????break;
????????????case 4:
????????????????sum=90;
????????????????break;
????????????case 5:
????????????????sum=120;
????????????????break;
????????????case 6:
????????????????sum=151;
????????????????break;
????????????case 7:
????????????????sum=181;
????????????????break;
? ? ? ? ? ? case 8:
????????????????sum=212;
????????????????break;
????????????case 9:
????????????????sum=243;
????????????????break;
????????????case 10:
????????????????sum=273;
????????????????break;
????????????case 11:
????????????????sum=304;
????????????????break;
????????????case 12:
????????????????sum=334;
????????????????break;
????????????default:
????????????????System.out.println("wrong input!");
????????????????return;
? ? ? ? }
????????sum = sum+day;
????????boolean leap;
????????if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
????????????leap = true;
????????} else {
????????????leap = false;
????????}
????????if(leap && month > 2) {
????????????sum++;
????????}
????????System.out.println("It is the "+sum+"th day.");
????}
}

import java.util.Scanner;
public class Demo14 {
????public static void main(String[] args) {
????????System.out.println("請輸入年 月 日:");
????????Scanner in = new Scanner(System.in);
????????int year = in.nextInt();
????????int month = in.nextInt();
????????int day = in.nextInt();
????????System.out.println("是該年的第"+count(year,month,day)+"天");
????}

?? ?public static int count(int year,int month,int day) {
????????int sum = 0;
????????int days = 0;
????????for(int i = 1;i < 3; 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;
? ? ? ? ? ? ?}
????????????sum+=days;
????????}
????????sum+=day;
????????return sum;
?????}
}


【程序15】 題目:輸入三個整數x,y,z,請把這三個數由小到大輸出。

1.程序分析:我們想辦法把最小的數放到x上,先將x與y進行比較,如果x>y則將x與y的值進行交換,然后再用x與z進行比較,如果x>z則將x與z的值進行交換,這樣能使x最小。

import java.util.Arrays;
import java.util.Scanner;
public class Demo15 {
????public static void main(String[] args) {
????????System.out.print("請輸入三個數:");
????????Scanner in = new Scanner(System.in);
????????int[] arr = new int[3];
????????for (int i = 0; i < 3; i++) {
????????????arr[i] = in.nextInt();
????????}
????????Arrays.sort(arr);
????????for (int i = 0 ; i < arr.length() ; i++) {
????????????System.out.print(arr[i] + " ");
????????}
????}
}

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;
}


【程序16】 題目:輸出9*9口訣乘法表。

1.程序分析:分行與列考慮,共9行9列,i控制行,j控制列。

出現重復的乘積(全矩形)
public class Demo16 {
????public static void main(String[] args) {
????????for (int i = 1; i <= 9; i++) {
????????????for (int j = 1; j <= 9; j++) {
????????????????System.out.print(i + "*" + j + "=" + (i*j) + "\t");
????????????}
????????????System.out.println();
????????}
????}
}

不現重復的乘積(下三角)
public class Demo16 {
????public static void main(String[] args) {
????????for (int i = 1; i <= 9; i++) {
????????????for (int j = 1; j <= i; j++) {
????????????????System.out.print(i + "*" + j + "=" + (i*j) + "\t");
????????????}
????????????System.out.println();
????????}
????}
}


【程序17】?? 題目:猴子吃桃問題:猴子第一天摘下若干個桃子,當即吃了一半,還不癮,又多吃了一個第二天早上又將剩下的桃子吃掉一半,又多吃了一個。以后每天早上都吃了前一天剩下的一半零一個。到第10天早上想再吃時,見只剩下一個桃子了。求第一天共摘了多少。

1.程序分析:采取逆向思維的方法,從后往前推斷。

public class Demo17 {
????public static void main(String[] args) {
????????int sum = 1;
????????for (int i = 0; i < 9; i++) {
????????????sum = (sum + 1) * 2;
????????}
????????System.out.println("第一天共摘"+sum);
????}
}


【程序18】?? 題目:兩個乒乓球隊進行比賽,各出三人。甲隊為a,b,c三人,乙隊為x,y,z三人。已抽簽決定比賽名單。有人向隊員打聽比賽的名單。a說他不和x比,c說他不和x,z比,請編程序找出三隊賽手的名單。

public class Demo18 {
????static char[] m = { 'a', 'b', 'c' };
????static char[] n = { 'x', 'y', 'z' };

????public static void main(String[] args) {
????????for (int i = 0; i < m.length; i++) {
????????????for (int j = 0; j < n.length; j++) {
????????????????if (m[i] == 'a' && n[j] == 'x') {
????????????????????continue;
????????????????} else if (m[i] == 'a' && n[j] == 'y') {
????????????????????continue;
????????????????} else if ((m[i] == 'c' && n[j] == 'x') || (m[i] == 'c' && n[j] == 'z')) {
????????????????????continue;
????????????????} else if ((m[i] == 'b' && n[j] == 'z') || (m[i] == 'b' && n[j] == 'y')) {
????????????????????continue;
????????????????} else {
? ? ? ? ? ? ? ? ? ? System.out.println(m[i] + " vs " + n[j]);
????????????????}
????????????}
????????}
????}
}

public class Demo18 {
????public String a, b, c;

????public Demo18(String a, String b, String c) {
????????this.a = a;
????????this.b = b;
????????this.c = c;
????}

?? ?public static void main(String[] args) {
????????Demo18 arr_a = new Demo18("a", "b", "c");
????????String[] b = { "x", "y", "z" };
????????for (int i = 0; i < 3; i++) {
????????????for (int j = 0; j < 3; j++) {
????????????????for (int k = 0; k < 3; k++) {
????????????????????Demo18 arr_b = new Demo18(b[i], b[j], b[k]);
????????????????????if (!arr_b.a.equals(arr_b.b) & !arr_b.b.equals(arr_b.c)
????????????????????????& !arr_b.c.equals(arr_b.a) & !arr_b.a.equals("x")
????????????????????????& !arr_b.c.equals("x") & !arr_b.c.equals("z")) {
????????????????????????System.out.println(arr_a.a + "--" + arr_b.a);
????????????????????????System.out.println(arr_a.b + "--" + arr_b.b);
????????????????????????System.out.println(arr_a.c + "--" + arr_b.c);
????????????????????}
????????????????}
????????????}
????????}
????}
}


【程序19】? 題目:打印出如下圖案(菱形)

1.程序分析:先把圖形分成兩部分來看待,前四行一個規律,后三行一個規律,利用雙重for循環,第一層控制行,第二層控制列。

三角形:
*
***
******
********
******
***
*

public class Demo19 {
????public static void main(String[] args) {
????????int i = 0;
????????int j = 0;
????????for ( i = 1; i <= 4; i++) {
????????????for ( j = 1; j <= 2 * i - 1; j++) {
????????????????System.out.print("*");
????????????}
????????????System.out.println();
????????}

?? ??? ?for ( i = 3; i >= 1; i--) {
????????????for ( j = 1; j <= 2 * i - 1; j++) {
????????????????System.out.print("*");
????????????}
????????????System.out.println();
????????}
????}
}

菱形:
? ? ?*
? ? ***
? ?*****
? *******
? ?*****
? ? ***
? ? ?*

public class Demo19 {
????public static void main(String[] args) {
????????int i = 0;
????????int j = 0;
????????for (i = 1; i <= 4; i++) ?{
????????????for (int k = 1; k <= 4 - i; k++) {
? ??????????????System.out.print( " " );
????????????}

????????????for (j = 1; j <= 2 * i - 1; j++) {
????????????????System.out.print("*");
????????????}
????????????System.out.println();
????????}

?? ??? ?for (i = 3; i >= 1; i--) {
????????????for (int k = 1; k <= 4 - i; k++) {
????????????????System.out.print( " " );
????????????}
????????for (j = 1; j <= 2 * i - 1; j++) {
????????????System.out.print("*");
????????}
????????System.out.println();
????????}
????}
}


【程序20】?? 題目:有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13...求出這個數列的前20項之和。

1.程序分析:請抓住分子與分母的變化規律。? ?

public class Demo20 {
????public static void main(String[] args) {
????????float fm = 1.0f;
????????float fz = 1.0f;
????????float temp;
????????float sum = 0f;
????????for (int i = 0; i < 20; i++) {
????????????temp = fm;
????????????fm = fz;
????????????fz = fz + temp;
????????????System.out.println((int) fz + "/" + (int) fm);
????????????sum += fz / fm;
????????}
????????System.out.println(sum);
????}
}


【程序21】?? 題目:求1+2!+3!+...+20!的和。

1.程序分析:此程序只是把累加變成了累乘。

public class Demo21 {
????public static void main(String[] args) {
????????long sum = 0;
????????long fac = 1;
????????for (int i = 1; i <= 20; i++) {
????????????fac = fac * i;
????????????sum += fac;
? ? ? ? }
? ? ? ? System.out.println(sum);
????}
}


【程序22】?? 題目:利用遞歸方法求5!。

1.程序分析:遞歸公式:f(n)=f(n-1)*4!

import java.util.Scanner;
public class Demo22 {
????public static long fac(int n) {
????????long value = 0;
????????if (n == 1 || n == 0) {
????????????value = 1;
????????} else if (n > 1) {
????????????value = n * fac(n - 1);
????????}
????????return value;
????}

?? ?public static void main(String[] args) {
????????System.out.println("請輸入一個數:");
????????Scanner in = new Scanner(System.in);
????????int n = in.nextInt();
????????System.out.println(n + "的階乘為:" + fac(n));
????}
}


【程序23】?? 題目:有5個人坐在一起,問第五個人多少歲?他說比第4個人大2歲。問第4個人歲數,他說比第3個人大2歲。問第三個人,又說比第2人大兩歲。問第2個人,說比第一個人大兩

歲。最后問第一個人,他說是10歲。請問第五個人多大?

1.程序分析:利用遞歸的方法,遞歸分為回推和遞推兩個階段。要想知道第五個人歲數,需知道第四人的歲數,依次類推,推到第一人(10歲),再往回推。

直接求解:

public class Demo23 {
????public static void main(String[] args) {
????????int n = 10;
????????for (int i = 0; i < 4; i++) {
????????????n = n + 2;
????????}
????????System.out.println("第五個人" + n + "歲");
????}
}

遞歸求解:

public class Demo23 {
????public static int getAge(int n) {
????????if (n == 1) {
????????????return 10;
????????}
????????return 2 + getAge(n - 1);
????}

?? ?public static void main(String[] args) {
????????System.out.println("第五個的年齡為" + getAge(5));
????}
}


【程序24】?? 題目:給一個不多于5位的正整數,要求:一、求它是幾位數,二、逆序打印出各位數字。?

本題原方法:

import java.util.Scanner;
public class Demo24 {
????public static void main(String[] args) {
????????Demo24 use = new Demo24();
????????System.out.println("請輸入:");
????????Scanner in = new Scanner(System.in);
????????long a = in.nextLong();
????????if (a < 0 || a >= 100000) {
????????????System.out.println("Error Input, please run this program Again!");
????????????System.exit(0);
????????}

?? ??? ?if (a >= 0 && a <= 9) {
????????????System.out.println(a + "是一位數");
????????????System.out.println("按逆序輸出是:"? + a);
????????} else if (a >= 10 && a <= 99) {
????????????System.out.println(a + "是二位數");
????????????System.out.println("按逆序輸出是:");
????????????use.converse(a);
????????} else if (a >= 100 && a <= 999) {
????????????System.out.println(a + "是三位數");
????????????System.out.println("按逆序輸出是:");
????????????use.converse(a);
????????} else if (a >= 1000 && a <= 9999) {
????????????System.out.println(a + "是四位數");
????????????System.out.println("按逆序輸出是:");
????????????use.converse(a);
????????} else if (a >= 10000 && a <= 99999) {
????????????System.out.println(a + "是五位數");
????????????System.out.println("按逆序輸出是:");
????????????use.converse(a);
????????}
????}

?? ?public void converse(long l) {
????????String s = Long.toString(l);
????????char[] ch = s.toCharArray();
????????for (int i = ch.length - 1; i >= 0; i--) {
????????????System.out.print(ch[i]);
????????}
????}
}

個人版方法:

import java.util.Scanner;
public class Demo24 {
????public static void main(String[] args) {
????????System.out.println("請輸入:");
????????Scanner in = new Scanner(System.in);
????????String str = in.next();
????????if (str.matches("\\d+")) { //正則表達式
????????????System.out.println("輸入的是" + str.length() + "位數");
????????????StringBuffer buf = new StringBuffer(str);
????????????System.out.println(buf.reverse());//字符串反轉
????????}
????}
}


【程序25】?? 題目:一個5位數,判斷它是不是回文數。即12321是回文數,個位與萬位相同,十位與千位相同。

原方法:

import java.util.Scanner;
public class Demo25 {
????static int[] a = new int[5];
????static int[] b = new int[5];
????public static void main(String[] args) {
????????boolean is = false;
????????System.out.println("Please input:");
????????Scanner in = new Scanner(System.in);
????????long l = in.nextLong();
????????if (l > 99999 || l < 10000) {
????????????System.out.println("Input error, please input again!");
????????????l = in.nextLong();
????????}

?? ??? ?for (int i = 4; i >= 0; i--) {
????????????a[i] = (int) (l / (long) Math.pow(10, i));
????????????l = (l % (long) Math.pow(10, i));
????????}
????????System.out.println();
????????for (int i = 0, j = 0; i < 5; i++, j++) {
????????????b[j] = a[i];
????????}
????????for (int i = 0, j = 4; i < 5; i++, j--) {
????????????if (a[i] != b[j]) {
????????????????is = false;
????????????????break;
????????????} else {
????????????????is = true;
? ? ? ? ? ? }
????????}

?? ??? ?if (is == false) {
????????????System.out.println("is not a Palindrom!");
????????} else if (is == true) {
????????????System.out.println("is a Palindrom!");
????????}
????}
}

個人版:

import java.util.Scanner;
public class Demo25 {
????public static void main(String[] args) {
????????System.out.println("請輸入:");
????????Scanner in = new Scanner(System.in);
????????String str = in.next();
????????int l = Integer.parseInt(str);//轉換成整數
????????if (l < 10000 || l > 99999) {
????????????System.out.println("輸入錯誤!");
????????????System.exit(0);
? ? ? ? }
????????boolean is=false;
????????char[] ch = str.toCharArray();

?? ??? ?for(int i=0; i < ch.length/2; i++) {
????????????if(ch[i] != ch[ch.length-i-1]){
????????????????is = false;
????????????} else {
????????????????is = true;
????????????}
????????}

?? ??? ?if(is) {
????????????System.out.println("這是一個回文!");
????????} else {
????????????System.out.println("不是一個回文!");
????????}
? ? ?}
}


【程序26】?? 題目:請輸入星期幾的第一個字母來判斷一下是星期幾,如果第一個字母一樣,則繼續判斷第二個字母。

1.程序分析:用情況語句比較好,如果第一個字母一樣,則判斷用情況語句或if語句判斷第二個字母。

import java.util.Scanner;
public class Demo26 {
????public static void main(String[] args) {
????????char weekSecond;//保存第二字母
????????Scanner in = new Scanner(System.in);//接收用戶輸入
????????System.out.println("請輸入星期的第一個字母:");
????????String letter = in.next();
????????if (letter.length() == 1) {//判斷用戶控制臺輸入字符串長度是否是一個字母
????????????char weekFirst = letter.charAt(0);//取第一個字符
????????????switch (weekFirst) {
????????????????case 'm':
????????????????case 'M':
????????????????????System.out.println("星期一(Monday)");
????????????????????break;
????????????????case 't':
????????????????case 'T':
????????????????System.out.print("由于星期二(Tuesday)與星期四(Thursday)均以字母T開頭,故需輸入第二個字母才能正確判斷:");
????????????????letter = in.next();
????????????????if (letter.length() == 1) {
????????????????????weekSecond = letter.charAt(0);
????????????????????if (weekSecond == 'U' || weekSecond == 'u') {
????????????????????????System.out.println("星期二(Tuesday)");
????????????????????????break;
????????????????????} else if (weekSecond == 'H' || weekSecond == 'h') {
????????????????????????System.out.println("星期四(Thursday)");
????????????????????????break;
????????????????????} else {
????????????????????????System.out.println("Error!");
????????????????????????break;
????????????????????}
????????????????} else {
????????????????????System.out.println("輸入錯誤,只能輸入一個字母,程序結束!");
????????????????????break;
????????????????}

? ? ? ? ? ?case 'w':
? ? ? ? ? ?case 'W':
????????????????System.out.println("星期三(Wednesday)");
????????????????break;
????????????case 'f':
????????????case 'F':
????????????????System.out.println("星期五(Friday)");
????????????????break;
????????????case 's':
????????????case 'S':
????????????????System.out.print("由于星期六(Saturday)與星期日(Sunday)均以字母S開頭,故需輸入第二個字母才能正確判斷:");
????????????????letter = in.next();
????????????????if (letter.length() == 1) {
????????????????????weekSecond = letter.charAt(0);
????????????????????if (weekSecond == 'A' || weekSecond == 'a') {
????????????????????????System.out.println("星期六(Saturday)");
????????????????????????break;
????????????????????} else if (weekSecond == 'U' || weekSecond == 'u') {
????????????????????????System.out.println("星期日(Sunday)");
????????????????????????break;
????????????????????} else {
????????????????????????System.out.println("Error!");
????????????????????????break;
????????????????????}
????????????????} else {
????????????????????System.out.println("輸入錯誤,只能輸入一個字母,程序結束!");
????????????????????break;
????????????????}

?? ??? ??? ?default:
????????????????System.out.println("輸入錯誤,不能識別的星期值第一個字母,程序結束!");
????????????????break;
? ? ? ? ? ?}
????????} else {
????????????System.out.println("輸入錯誤,只能輸入一個字母,程序結束!");
????????}
????}
}


【程序27】?? 題目:求100之內的素數

public class Demo27{
????public static void main(String args[]) {
????????int w = 1;
????????for(int i = 2; i <= 100; i++) {
????????????for(int j = 2; j < i; j++) {
????????????????f(i % j?== 0) {
????????????????????break;
????????????????}
????????????}
? ? ? ? ? ? System.out.println(i+"是素數");
????????}
????}
}


【程序28】?? 題目:對10個數進行排序。

1.程序分析:可以利用選擇法,即從后9個比較過程中,選擇一個最小的與第一個元素交換,下次類推,即用第二個元素與后8個進行比較,并進行交換。

本例代碼為生成隨機10個數排序,并輸入1個數,插入重排序輸出:

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Demo28 {

?? ?public static void main(String[] args) {
????????int arr[] = new int[11];
????????Random r = new Random();
????????for (int i = 0; i < 10; i++) {
????????????arr[i] = r.nextInt(100) + 1; //得到10個100以內的整數
????????}
????????Arrays.sort(arr);
????????for (int i = 0; i < arr.length; i++) {
????????????System.out.print(arr[i] +"\t");
????????}
????????System.out.print("\nPlease Input a int number:" );

?? ??? ?Scanner in = new Scanner(System.in);
????????arr[10] = in.nextInt();
????????Arrays.sort(arr);
????????for (int i = 0; i < arr.length; i++) {
????????????System.out.print(arr[i] +"\t");
????????}
????}
}

個人代碼:

import java.util.Arrays;
import java.util.Scanner;
public class Demo28 {
????public static void main(String[] args) {
????????System.out.println("請輸入10個數:");
????????Scanner in = new Scanner(System.in);
????????int[] arr = new int[10];
????????for (int i = 0; i < 10; i++) {
????????????arr[i] = in.nextInt();
????????}
????????System.out.println("原數組為:");
????????for (int x : arr) { ?//foreach遍歷
????????????System.out.print( x + "\t");
????????}
????????Arrays.sort(arr);
????????System.out.println();
????????System.out.println("排序后為:");
????????for (int i = 0; i < arr.length; i++) {
????????????System.out.print(arr[i] + "\t");
????????}
????}
}


【程序29】?? 題目:求一個3*3矩陣主對角線元素之和。

1.程序分析:利用雙重for循環控制輸入二維數組,再將a[i][i]累加后輸出。

public class Demo29 {
????public static void main(String[] args) {
????????double sum = 0;
????????int array[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 7, 8 } };
????????for (int i = 0; i < 3; i++) {
????????????for (int j = 0; j < 3; j++) {
????????????????if (i == j) {
????????????????????sum = sum + array[i][j];
????????????????}
????????????}
????????????System.out.println(sum);
????????}
????}
}


【程序30】?? 題目:有一個已經排好序的數組。現輸入一個數,要求按原來的規律將它插入數組中。

1.程序分析:首先判斷此數是否大于最后一個數,然后再考慮插入中間的數的情況,插入后此元素之后的數,依次后移一個位置。

import java.util.Random;
public class Demo30 {
????public static void main(String[] args) {
????????int temp = 0;
????????int arr[] = new int[12];
????????Random r = new Random();
????????for (int i = 0; i <= 10; i++) {
????????????arr[i] = r.nextInt(1000);
????????}
????????for (int i = 0; i <= 10; i++) {
????????????System.out.print(arr[i] + "\t");
????????}
????????for (int i = 0; i <= 9; i++) ?{
????????????for (int k = i + 1; k <= 10; k++) {
????????????????if (arr[i] > arr[k]) {
????????????????????temp = arr[i];
????????????????????arr[i] = arr[k];
????????????????????arr[k] = temp;
????????????????}
????????????}
? ? ? ? }
? ? ? ? System.out.println();
? ? ? ? for (int k = 0; k <= 10; k++) {
????????????System.out.print(arr[k] + "\t");
????????}
????????arr[11] = r.nextInt(1000);
????????for (int k = 0; k <= 10; k++) {
????????????if (arr[k] > arr[11]) {
????????????????temp = arr[11];
????????????????for (int j = 11; j >= k + 1; j--) {
????????????????????arr[j] = arr[j - 1];
????????????????????arr[k] = temp;
????????????????}
????????????}
????????}
????????System.out.println();
????????for (int k = 0; k <= 11; k++) {
????????????System.out.print(arr[k] + "\t");
????????}
????}
}


【程序31】?? 題目:將一個數組逆序輸出。

用逆序循環控制變量輸出:

public class Demo31 {
? ? public static void main(String[] args) {
????????int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
????????for (int i = a.length - 1; i >= 0; i--) {
????????????System.out.print(a[i] + " ");
????????}
????}
}


【程序32】?? 題目:取一個整數a從右端開始的第4~7位數字。

import java.util.*;
public class Demo32 {
????public static void main(String[] args) {
????????Scanner in = new Scanner(System.in);
????????System.out.print("請輸入一個7位以上的正整數:");
????????long l = in.nextLong();
????????String str = Long.toString(l);
????????char[] ch = str.toCharArray();
????????int j=ch.length;
????????if (j < 7) {
????????????System.out.println("輸入錯誤!");
????????} else {
????????????System.out.println("截取從右端開始的4~7位是:"+ch[j-7]+ch[j-6]+ch[j-5]+ch[j-4]);
????????}
????}
}


【程序33】?? 題目:打印出楊輝三角形(要求打印出10行如下圖)

1.程序分析:
1
1?? 1
1?? 2?? 1
1?? 3?? 3?? 1
1?? 4?? 6?? 4?? 1
1?? 5?? 10?? 10?? 5?? 1

public class Demo33 {
????public static void main(String args[]) {
????????int i, j;
????????int a[][];
????????int n = 10;
????????a = new int[n][n];
????????for (i = 0; i < n; i++) {
????????????a[i][i] = 1;
????????????a[i][0] = 1;
????????}
????????for (i = 2; i < n; i++) {
????????????for (j = 1; j <= i - 1; j++) {
????????????????a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
????????????}
????????}
????????for (i = 0; i < n; i++) {
????????????for (j = 0; j <= i; j++) {
????????????????System.out.printf(a[i][j] + "\t");
????????????}
????????????System.out.println();
????????}
????}
}


【程序34】?? 題目:輸入3個數a,b,c,按大小順序輸出。
import java.util.Arrays;
public class Demo34 {
????public static void main(String[] args) {
????????int[] arrays = { 800, 56, 500 };
? ? ? ? Arrays.sort(arrays);
????????for (int n = 0; n < arrays.length; n++)
????????????System.out.println(arrays[n]);
????}
}


【程序35】?? 題目:輸入數組,最大的與第一個元素交換,最小的與最后一個元素交換,輸出數組。

import java.util.*;
public class Demo35 {
????public static void main(String[] args) {
????????int i, min = 0, max = 0, n, temp1, temp2;
????????int a[];
????????System.out.println("定義數組的長度:");
????????Scanner in = new Scanner(System.in);
????????n = in.nextInt();
????????a = new int[n];
????????for (i = 0; i < n; i++) {
????????????System.out.print("輸入第" + (i + 1) + "個數據:");
????????????a[i] = in.nextInt();
????????}
????????for (i = 1; i < n; i++) {
????????????if (a[i] > a[max]) {
????????????????max = i;
????????????}
????????????if (a[i] < a[min]) {
????????????????min = i;
????????????}
????????}
????????temp1 = a[0];
????????a[0] = a[max];
????????a[max] = temp1;
????????temp2 = a[min];?
????????if (min != 0) { // 如果最小值不是a[0],執行下面
????????????a[min] = a[n - 1];
????????????a[n - 1] = temp2;?
????????} else {? //如果最小值是a[0],執行下面
????????????a[max] = a[n - 1];
????????????a[n - 1] = temp1;
????????}
????????for (i = 0; i < n; i++) {?
????????????System.out.print(a[i] + " " );
????????}
????}
}


【程序36】?? 題目:有n個整數,使其前面各數順序向后移m個位置,最后m個數變成最前面的m個數

import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Demo36 {
????public static void main(String[] args) {
????????Scanner in = new Scanner(System.in);
????????System.out.println("輸入數字個數n:");
????????int n = in.nextInt();
????????System.out.println("輸入后移位數m:");
????????int m = in.nextInt();
????????LinkedList list = new LinkedList();
????????for (int i = 0; i < n; i++) {
????????????System.out.println("請輸入第"+(i+1)+"個數:");
????????????list.add(in.nextInt());
????????}
????????System.out.println("原數據排序為:");
????????for (int t : list) {
????????????System.out.print(t + " " );
????????}
????????System.out.println();
????????List temp1 = list.subList(list.size() - m, list.size());????????
????????List temp2 = list.subList(0, list.size() - m);
????????temp2.addAll(0, temp1);
????????System.out.println("移動后排序為;");
????????for (int t : temp2) {
????????????System.out.print(t + " " );
????????}
????}
}

import java.util.*;
public class Demo36 {
????public static void main(String[] args) {
????????Scanner in = new Scanner(System.in);? ? ? ?
????????System.out.println("請定義數組的長度:");
????????int n = in.nextInt();
????????System.out.println("請輸入移動的位數:");
????????int m = in.nextInt();
????????int [] arr = new int [n];
????????int [] brr = new int [n];
????????for(int i = 0;i < n;i ++) {
????????????System.out.println("請輸入第"+(i+1)+"個數:");
????????????arr[i] = in.nextInt();
????????}
????????System.out.println("排序前:");
????????for(int i = 0;i < n;i ++) {
????????????System.out.print(arr[i]+" ");
????????}
????????System.out.println();
????????for(int i = 0;i < m;i ++) {
????????????brr[i] = arr[n - m + i];
????????}
????????for(int i = 0;i < n - m;i ++) {
????????????arr[m + i] = arr[i];
????????}
????????for(int i=0;i < m;i ++) {
????????????arr[i] = brr[i];
????????}
????????System.out.println("排序后:");
????????for(int i = 0;i < m;i ++) {
????????????System.out.print(arr[i]+" ");
????????}
????}
}


【程序37】?? 題目:有n個人圍成一圈,順序排號。從第一個人開始報數(從1到3報數),凡報到3的人退出圈子,問最后留下的是原來第幾號的那位。
(約瑟夫環問題,百度百科有時間復雜度最簡單的數學方法)
原例代碼:

import java.util.Scanner;
public class Demo37 {
????public static void main(String[] args) {
????????System.out.println("請輸人數n:");
????????Scanner in = new Scanner(System.in);
????????int n = in.nextInt();
????????boolean[] arr = new boolean[n];
????????for (int i = 0; i < arr.length; i++) {
????????????arr[i] = true; //下標為TRUE時說明還在圈里
????????}
????????int leftCount = n;
????????int countNum = 0;
????????int index = 0;
????????while (leftCount > 1) {
????????????if (arr[index] == true) { //當在圈里時
????????????????countNum++;? //報數遞加
????????????????if (countNum == 3) { //報數為3時
????????????????????countNum = 0; //從零開始繼續報數
? ? ? ? ? ? ? ? ? ? arr[index] = false; //此人退出圈子
? ? ? ? ? ? ? ? ? ? leftCount--; //剩余人數減一
????????????????}
????????????}
????????????index++; //每報一次數,下標加一
????????????if (index == n) { //是循環數數,當下標大于n時,說明已經數了一圈,
????????????????index = 0; //將下標設為零重新開始。
? ? ? ? ? ? }
????????}
????????for (int i = 0; i < n; i++) {
????????????if (arr[i] == true) {
????????????????System.out.println(i);
????????????}
????????}
????}
}

個人代碼1:
import java.util.Scanner;
public class Demo37 {
????public static void main(String[] args) {
????????System.out.println("請輸入人數:");
????????Scanner in = new Scanner(System.in);
????????int[] a = new int[in.nextInt()];
????????for (int i = 0; i < a.length; i++) {
????????????a[i] = 1;
????????}
????????int left = a.length;
????????int j = 0;
????????int num = 0;
????????while (left > 1) {
????????????if (a[j] == 1) {
????????????????num ++;
????????????}
`? ? ? ? ? ?if (num == 3) {
????????????????a[j] = 0;
????????????????num = 0;
????????????????left--;
????????????}
? ? ? ? ? ? j++;
????????????if (j == a.length) {
????????????????j = 0;
????????????}
????????}
????????for (int i = 0; i < a.length; i++) {
????????????if (a[i] == 1) {
????????????????System.out.println("最后留下的人是"+ (i + 1) + "號");
????????????????break;
????????????}
? ? ? ? }
????}
}

個人代碼2:
import java.util.LinkedList;
import java.util.Scanner;
public class Demo37 {
????public static void main(String[] args) {
????????LinkedList l = new LinkedList();
? ? ? ? System.out.println("請輸入人數:");
? ? ? ? Scanner in = new Scanner(System.in);
????????int len = in.nextInt();
????????for (int i = 0; i < len; i++) {
????????????l.add(i + 1);
????????}
????????int sum = 0;
????????int temp = 0;
????????for (int i = 0; sum != len - 1;) {
????????????if (l.get(i) != 0) {
????????????????temp++;
????????????}
????????????if (temp == 3) {
????????????????l.remove(i);
????????????????l.add(i, 0);
????????????????temp = 0;
????????????????sum++;
????????????}
????????????i++;
????????????if (i == l.size()) {
????????????????i = 0;
????????????}
????????}
????????for (int t : l) {
????????????if (t != 0) {
????????????????System.out.println("最后留下的人是" + t + "號");
????????????}
????????}
????}
}


【程序38】?? 題目:寫一個函數,求一個字符串的長度,在main函數中輸入字符串,并輸出其長度。

import java.util.Scanner;
public class Demo38 {
????public static void main(String[] args) {
????????Scanner in = new Scanner(System.in);
????????System.out.println("請輸入一個字符串:");
????????String mys = in.next();
????????System.out.println(str_len(mys));
????}
????public static int str_len(String x) {
????????return x.length();
????}
}


【程序39】? 題目:編寫一個函數,輸入n為偶數時,調用函數求1/2+1/4+...+1/n,當輸入n為奇數時,調用函數1/1+1/3+...+1/n

import java.util.Scanner;
public class Demo39 {
????public static double ouShu(int n) {
????????double result = 0;
????????for (int i = 2; i <= n; i = i + 2) {
????????????result +=? 1 / (double) i;
????????}
????????return result;
????}
????public static double jiShu(int n) {
????????double result = 0;
????????for (int i = 1; i <= n; i = i + 2) {
????????????result += 1 / (double) i;
????????}
????????return result;
????}
????public static void main(String[] args) {
????????Scanner in = new Scanner(System.in);
????????System.out.println("輸入n的值:");
????????int n = in.nextInt();
????????if (n % 2 == 0) { //偶數,1/2+1/4+...+1/n
????????????System.out.println(ouShu(n));
????????} else { //奇數,1/1+1/3+...+1/n
????????????System.out.println(jiShu(n));
????????}
????}
}


【程序40】? 題目:字符串排序。

(利用容器類中的sort方法)
import java.util.*;
public class Demo40 {
????public static void main(String[] args) {
????????ArrayList list = new ArrayList();?
????????list.add("010102");
????????list.add("010003");
????????list.add("010201");
????????Collections.sort(list);
????????for (int i = 0; i < list.size(); i++) {
????????????System.out.println(list.get(i));
????????}
????}
}

import java.util.*;
public class Demo40 {
????public static void main(String[] args){
????????Scanner in = new Scanner(System.in);
????????System.out.println("請定義字符串的個數:");
????????int n = n.nextInt();
????????String[] str = new String[n];
????????for(int i = 0; i < str.length; i++) {
????????????System.out.println("請輸入第"+(i+1)+"字符串:");
????????????str[i] = in.next();
????????}
????????strSort(n,str);
????????System.out.println("字符串排序后:");
????????for(int i = 0; i < str.length; i++) {
????????????System.out.print(str[i]+" ");
????????}
????}
????public static void strSort(int n,String[] arr) {
????????for(int i=0; i < n; i++ ) {
????????????for(int j=i+1; j < n; j++) {
????????????????if(compare(arr[i], arr[j]) == false) {
????????????????????String temp = arr[i];
????????????????????arr[i] = arr[j];
????????????????????arr[j] = temp;
????????????????}
????????????}
????????}
????}
? ?static boolean compare(String s1, String s2) {
????????boolean result = true;
????????for(int i=0; i < s1.length && i < s2.length; i++) {
????????????if(s1.charAt(i) > s2.charAt(i)) {
????????????????result = false;
????????????????break;
????????????} else if(s1.charAt(i) < s2.chatAt(i)) {
????????????????result = true;
????????????????break;
????????????} else {
????????????????if(s1.length() < s2.length()) {
????????????????????result = true;
????????????????} else {
????????????????????result = false;
? ? ? ? ? ? ? ? }
????????????}
????????}
????????return result;
????}
}


【程序41】?? 題目:海灘上有一堆桃子,五只猴子來分。第一只猴子把這堆桃子平均分為五份,多了一個,這只猴子把多的一個扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一個,它同樣把多的一個扔入海中,拿走了一份,第三、第四、第五只猴子都是這樣做的,問海灘上原來最少有多少個桃子?

本題源碼:
public class Demo41 {
????static int ts = 0;// 桃子總數
????int fs = 1;// 記錄分的次數
????static int hs = 5;// 猴子數
????int tsscope = 5000;// 桃子數的取值范圍,太大容易溢出
????public int fT(int t) {
????????if (t == tsscope) {
????????????// 當桃子數到了最大的取值范圍時取消遞歸
????????????System.out.println("結束");
????????????return 0;
????????} else {
????????????if ((t - 1) % hs == 0 && fs <= hs) {
????????????????if (fs == hs) {
????????????????????System.out.println("桃子數=" + ts + "時滿足分桃條件");
????????????????}
????????????????fs += 1;
????????????????return fT((t - 1) / 5 * 4);// 返回猴子拿走一份后的剩下的總數
? ? ? ? ? ? } else {
????????????????// 沒滿足條件
????????????????fs = 1;// 分的次數重置為1
????????????????return fT(ts += 1);// 桃子數加+1
????????????}
????????}
????}

?? ?public static void main(String[] args) {
? ? ? ? ? ?new Demo41().fT(0);
????}
}

個人修改:
public class Demo41 {
????public static void main(String[] args) {
????????int sum = 0;
????????for (int i = 6;; i++) {// 最少6個分最后一次
????????????sum = i;// 桃子數
????????????for (int j = 0; j < 5; j++) {// 分的次數循環
????????????????if ((sum - 1) % 5 == 0 && j < 5) {// 如果扔一個后能均分5份,繼續分
????????????????????sum = (sum - 1) / 5 * 4;// 每分一次剩余桃子數
????????????????????????if (j == 4) {// 如果已分5次,且仍能除盡,輸出,退出程序
????????????????????????????System.out.println(i);
????????????????????????????System.exit(0);
????????????????????????}
? ? ? ? ? ? ? ? }
????????????}
????????}
????}
}


【程序42】?? 題目:809*??=800*??+9*??+1。其中??代表的兩位數,8*??的結果為兩位數,9*??的結果為3位數。求??代表的兩位數,及809*??后的結果。
(本題為無解,去掉1有解)
public class Demo42 {
????public static void main(String[] args) {
????????for (int i = 10; i < 100; i++) {
????????????if (809 * i == (800 * i + 9 * i + 1) && 8 * i >= 10 && 8 * i < 100
????????????????&& 9 * i >= 100 && 9 * i < 1000) {
????????????????System.out.println("?? =" + i);
????????????????System.out.println("809*??="+ 809 * i);
? ? ? ? ? ? ? ? System.exit(0);
????????????}
? ? ? ?}
????}
}


【程序43】?? 題目:求0—7所能組成的奇數個數。

暴力算法:
public class Demo43 {
????public static boolean isJiShu(int n) {
????????if (n % 2 != 0) {
????????????return true;
????????} else {
????????????return false;
????????}
????}
????public static boolean fun(char c) {
? ? ????if (c >= '0' && c <= '7') {
????????????return true;
????????} else {
????????????return false;
????????}
????}
????public static void main(String[] args) {
????????int count = 0;
????????String s;
????????for (int i = 0; i < 100000000; i++) {
????????????s = "" + i;
????????????boolean flag = true;
????????????char[] c = s.toCharArray();
????????????for (int j = 0; j < c.length; j++) {
????????????????if (!fun(c[j])) {
????????????????????flag = false;
????????????????????break;
????????????????}
????????????}
????????????if (flag && isJiShu(i)) {
????????????????count++;
????????????}
????????????s = "";
????????}
? ? ? ? System.out.println("共" + count + "個。");
????}
}

數學算法:
public class Demo43 {
????public static void main(String[] args) {
????????// 因為是奇數,所以個位只能是1,3,5,7共4種,前面可隨便排列
????????int count = 4;// 個位的4種
????????// 2位時,十位有8種,個位4種,8×4
????????// 3位時,8×8×4……
????????for (int i = 1; i < 8; i++) {
????????????count = 8 * count;
????????????System.out.println("count:" + count);
????????}
????}
}

個人算法:
//組成1位數是4個
//組成2位數是7*4個
//組成3位數是7*8*4個
//組成4位數是7*8*8*4個
//......

public class Demo43 {
????public static void main (String[] args) {
????????int sum=4;
????????int j;
????????System.out.println("組成1位數是 "+sum+" 個");
????????sum=sum*7;
????????System.out.println("組成2位數是 "+sum+" 個");
????????for(j = 3; j <= 9; j++) {
????????????sum=sum*8;
????????????System.out.println("組成"+j+"位數是 "+sum+" 個");
????????}
????}
}


【程序44】?? 題目:一個偶數總能表示為兩個素數之和。(注:哥德巴赫猜想是想證明對任何大于6的自然數n之內的所有偶數可以表示為兩個素數之和)

public class Demo44 {
????public static boolean isSuShu(int x) {
????????if (x == 0 || x == 1) {
????????????return false;
????????}
????????for (int i = 2; i <= Math.sqrt(x); i++) {
????????????if (x % i == 0) {
????????????????return false;
????????????}
????????}
????????return true;
????}

?? ?public static void main(String[] args) {
????????// 求了下100以內的情況
????????for (int i = 0; i < 100; i = i + 2) {
????????????for (int j = 0; j <= (i + 1) / 2; j++) {
????????????????if (isSuShu(j) && isSuShu(i - j)) {
????????????????????System.out.println(i + "=" + j + "+" + (i - j));
????????????????}
????????????}
????????}
????}
}


public class Demo44{? ?
????public static void main(String[] args) {? ?
????????for (int i=6;i<=100 ;i+=2 ) {? ?
????????????for (int j=2;j<100 ;j++ ) {? ? ? ?
????????????????if (!isPrime(j) || !isPrime(i-j) || j >= i) {
????????????????????continue;
????????????????}
????????????????System.out.println(i + "=" + j + "+" + (i-j));? ? ? ? ? ?
????????????????break;? ? ? ?
????????????}? ? ? ?
????????}? ?
????}? ?

?? ?public static boolean isPrime(int n) {? ?
????????for (int i = 2; i < n; i++) {
????????????if(n % i == 0) {
????????????????return false;
????????????}? ?
????????}? ? ? ? ? ?
????????return true;? ?
????}
}


【程序45】?? 題目:(1)判斷幾個9能被一個素數整除。(2)判斷一個整數能被幾個9整除。(原題:一個素數能被幾個9整除)

(一)
public class Demo45 {
????public static boolean isSuShu(int x) {
????????if (x == 0 || x == 1) {
????????????return false;
????????}
????????for (int i = 2; i <= Math.sqrt(x); i++) {
????????????if (x % i == 0) {
????????????????return false;
????????????}
????????}
????????return true;
????}

?? ?public static void main(String[] args) {
????????int[] a = new int[100];
????????int n = 0;
????????int num = 0;
????????// 長度100的素數數組
????????while (n < 100) {
????????????if (isSuShu(num)) {
????????????????a[n] = num;
? ? ? ? ? ? ? ? n++;
????????????????num++;
????????????} else {
????????????????num++;
????????????}
????????}
????????/* for (int t : a) {
????????System.out.println(t);
????????}*/
????????String s = "9";
????????int index = 0;
????????while (s.length() < 9) {
????????????if (new Integer(s).intValue() % a[index] == 0) {
????????????????System.out.println(s + "%" + a[index] + "=0");
????????????????if (index < 100 - 1) {
????????????????????index++;
????????????????} else {
????????????????????index = 0;
????????????????????s = s + "9";
? ? ? ? ? ? ? ? }
????????????????// System.exit(0);
? ? ? ? ? ? } else {
????????????????if (index < 100 - 1) {
????????????????????index++;
????????????????} else {
????????????????index = 0;
????????????????s = s + "9";
????????????}
????????}
? ? ?}
? }
}

(二)
import java.util.*; public class Demo45 {
????public static void main (String[] args) {
????????Scanner in = new Scanner(System.in);
????????System.out.print("請輸入一個整數:");
????????int num = in.nextInt();
????????int tmp = num;
????????int count = 0;
????????for(int i = 0 ; tmp % 9 == 0 ;) {
????????????tmp = tmp / 9;
????????????count ++;
????????}
????????System.out.println(num+" 能夠被 "+count+" 個9 整除。");
????}
}


【程序47】?? 題目:讀取7個數(1—50)的整數值,每讀取一個值,程序打印出該值個數的

import java.util.*;
public class Demo47 {
????public static void main(String[] args) {
????????Scanner s = new Scanner(System.in);
????????int n = 1,num;
????????while(n <= 7) {
????????????do {
????????????????System.out.print("請輸入一個1--50 之間的整數:");
????????????????num = s.nextInt();
????????????} while(num < 1 || num > 50);
????????????for(int i = 1;i <= num; i++) {
????????????????System.out.print("*");
? ? ? ? ? ? }
????????????System.out.println();
????????????n ++;
????????}
????}
}

import java.util.Scanner;
public class Demo47 {
????public static void print(int n) {
????????for (int i = 0; i < n; i++) {
????????????System.out.print("*");
????????}
????????System.out.println();
????}

?? ?public static void main(String[] args) {
????????Scanner in = new Scanner(System.in);
????????for (int i = 0; i < 7; i++) {
????????????int temp = in.nextInt();
????????????print(temp);
????????}
????}
}


【程序48】?? 題目:某個公司采用公用電話傳遞數據,數據是四位的整數,在傳遞過程中是加密的,加密規則如下:每位數字都加上5,然后用和除以10的余數代替該數字,再將第一位和第四位交換,第二位和第三位交換。

import java.util.Scanner;
public class Demo48{
????public static void main(String[] args) {
????????Scanner in = new Scanner(System.in);
????????System.out.println("請輸入一個4位數字:");
????????String str = in.next();
????????if (!((str).matches("\\d{4}"))) {
????????????System.out.println("輸入的不是4位數字!");
????????????System.exit(0);
????????}
????????char[] c = str.toCharArray();
????????int[] a = new int[4];
????????for (int i = 0; i < a.length; i++) {
????????????a[i] = ((int) (c[i] - '0') + 5) % 10;
????????}
????????int t;
????????t = a[0];
????????a[0] = a[3];
????????a[3] = t;
????????t = a[1];
????????a[1] = a[2];
????????a[2] = t;
????????System.out.println("結果是:" + a[0] + a[1] + a[2] + a[3]);
????}
}

import java.util.*;
public class Demo48 {
????public static void main(String args[]) {
????????Scanner s = new Scanner(System.in);
????????int num=0,temp;
????????do {
????????????System.out.print("請輸入一個4位正整數:");
????????????num = s.nextInt();
????????} while (num < 1000 || num > 9999);
????????????int a[] = new int[4];
????????????a[0] = num/1000; //取千位的數字
????????????a[1] = (num/100) % 10; //取百位的數字
????????????a[2] = (num/10) % 10; //取十位的數字
????????????a[3] = num % 10; //取個位的數字
????????????for(int j = 0;j < 4; j++) {
????????????????a[j] += 5;
????????????????a[j] %= 10;
????????????}
????????????for(int j= 0; j<= 1; j++) {
????????????????temp = a[j]; a[j] = a[3-j]; a[3-j] = temp;
????????????}
????????????System.out.print("加密后的數字為:");
????????????for(int j = 0; j < 4; j++) {
????????????????System.out.print(a[j]);
????????????}
? ? ? }
}


【程序49】?? 題目:計算字符串中子串出現的次數。

import java.util.Scanner;
public class Demo49 {
????public static void main(String[] args) {
????????Scanner in = new Scanner(System.in);
????????System.out.println("請輸入主串:");
????????String str1 = in.nextLine();
????????System.out.println("請輸入子串:");
????????String str2 = in.nextLine();
????????// 生成子串長度的N個字符串數組
????????String[] sa = new String[str1.length() - str2.length() + 1];
????????for (int i = 0; i < sa.length; i++) {
????????????sa[i] = str1.substring(i, i + str2.length());
????????}
????????int sum = 0;
????????// 子串與N個拆開的子串比對
????????for (int i = 0; i < sa.length; i++) {
????????????if (sa[i].equals(str2)) {
????????????????// 成功配對,計數器+1;
????????????????sum++;
????????????????// 因為不計算重疊的子串,所以跳過配對之后的部分拆分子串
????????????????i = i + str2.length();
????????????}
????????}
????????System.out.println("主串中共包含" + sum + "個字串");
????}
}

【程序50】??? 題目:有五個學生,每個學生有3門課的成績,從鍵盤輸入以上數據(包括學生號,姓名,三門課成績),計算出平均成績,把原有的數據和計算出的平均分數存放在磁盤文件

import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
class Student {
????private int number = 0;
????private String name = "";
????private double[] a = new double[3];
????public double getAve() {
????????return (a[0] + a[1] + a[2]) / 3;
????}

?? ?public Student(int number, String name, double[] a) {
????????super();
????????this.number = number;
????????this.name = name;
????????this.a = a;
????}

?? ?@Override
????public String toString() {
????????return "學號:" + this.number + "\t姓名:" + this.name + "\r\n各科成績:\r\n" + a[0] + "\t" + a[1] + "\t" + a[2] + "\r\n平均成績:\r\n" + this.getAve();
????}
}

public class Demo50 {
????public static Student input() {
????????Scanner s = new Scanner(System.in);
? ? ? ? System.out.println("請輸入學號:");
????????int num = s.nextInt();
????????System.out.println("請輸入姓名:");
????????String name = s.next();
????????System.out.println("請分別輸入3門成績:");
????????double[] a = new double[3];
????????for (int i = 0; i < 3; i++) {
????????????a[i] = s.nextDouble();
????????}
????????return new Student(num, name, a);
????}

?? ?public static void main(String[] args) throws Exception {
????????Student[] st = new Student[2];
????????for (int i = 0; i < st.length; i++) {
????????????st[i] = input();
????????}
????????File f = new File("d:" + File.separator + "123.txt");
????????FileWriter output = new FileWriter(f);
????????for (int i = 0; i < st.length; i++) {
????????????output.write(st[i].toString() + "\r\n");
????????????output.write("\r\n");
????????}
????????output.close();
????}
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一...
    阿里高級軟件架構師閱讀 3,314評論 0 19
  • 【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔...
    葉總韓閱讀 5,167評論 0 41
  • Java經典問題算法大全 /*【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子...
    趙宇_阿特奇閱讀 1,916評論 0 2
  • 50道經典Java編程練習題,將數學思維運用到編程中來。抱歉哈找不到文章的原貼了,有冒犯的麻煩知會聲哈~ 1.指數...
    OSET我要編程閱讀 7,173評論 0 9
  • 昨天,范偉摘得金馬影帝讓很多人感到意外,但是細想想又覺得實至名歸。雖然他和小李子獲奧斯卡影帝的狀況不太一樣,但是沒...
    彼得貓烏金閱讀 266評論 0 1