Java經(jīng)典問題算法(一)

/*

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

1.

程序分析: 兔子的規(guī)律為數(shù)列1,1,2,3,5,8,13,21....

*/

package cn.com.flywater.FiftyAlgorthm;

public class FirstRabbit {

public static final int MONTH = 15;

public static void main(String[] args) {

?? long f1 = 1L, f2 = 1L;

?? long f;

?? for(int i=3; i

??? f = f2;

??? f2 = f1 + f2;

??? f1 = f;

??? System.out.print("

第"

+ i +"個月的兔子對數(shù):");

??? System.out.println(" " +f2);

?? }

}

}

/*【程序2】

*

作者 南楓題目:判斷101-200之間有多少個素數(shù),并輸出所有素數(shù)。

1.

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

package cn.com.flywater.FiftyAlgorthm;

public class SecondPrimeNumber {

public static int count = 0;

?public static void main(String[] args) {

???? for(int i=101;i<200;i++){

??? ?????boolean b = true;//

默認(rèn)此數(shù)就素數(shù)

???????? for(intj=2;j<=Math.sqrt(i);j++){

???????????? if(i%j==0){

???????????????? b = false; //

此數(shù)不是素數(shù)

???????????????? break;

???????????? }

???????? }

???????? if(b){

???????????? count++;

???????????? System.out.print(i+"");

???????? }

???? }

???? System.out.println("\n

素數(shù)的個數(shù):"+count);

??? }

}

/*

【程序3】作者 南楓題目:打印出所有的"水仙花數(shù)(narcissus

number)",所謂"水仙花數(shù)"是指一個三位數(shù),其各位數(shù)字立方和等于該數(shù)本身。例如:153是一個"水仙花數(shù)",因為153=1的三次方+5的三次方+3的三次方。

1.

程序分析:利用for循環(huán)控制100-999個數(shù),每個數(shù)分解出個位,十位,百位。*/

package cn.com.flywater.FiftyAlgorthm;

public class ThirdNarcissusNum {

static int b, bb, bbb;

public static void main(String[] args) {

?? for(int num=101; num<1000; num++) {

??? ThirdNarcissusNum tnn = newThirdNarcissusNum();

??? tnn.f(num);

?? }

}

public void f(int m) {

?? bbb = m / 100;

?? bb = (m % 100) / 10;

?? b = (m % 100) % 10;

?? if((bbb * bbb * bbb + bb * bb * bb + b* b * b) == m) {

??? System.out.println(m);

?? }

}

}

/*

【程序4】作者 南楓題目:將一個正整數(shù)分解質(zhì)因數(shù)。例如:輸入90,打印出90=2*3*3*5。程序分析:對n進(jìn)行分解質(zhì)因數(shù),應(yīng)先找到一個最小的質(zhì)數(shù)k,然后按下述步驟完成:

(1)

如果這個質(zhì)數(shù)恰等于n,則說明分解質(zhì)因數(shù)的過程已經(jīng)結(jié)束,打印出即可。

(2)

如果n>k,但n能被k整除,則應(yīng)打印出k的值,并用n除以k的商,作為新的正整數(shù)你n,重復(fù)執(zhí)行第一步。

(3)

如果n不能被k整除,則用k+1作為k的值,重復(fù)執(zhí)行第一步。*/

package cn.com.flywater.FiftyAlgorthm;

import java.util.Scanner;

public class FourthPrimeFactor {

static int n, k = 2;

public static void main(String[] args) {

?? Scanner s = new Scanner(System.in);

?? n = s.nextInt();

?? System.out.print(n + "=" );

?? FourthPrimeFactor fpf = newFourthPrimeFactor();

?? fpf.f(n);

}

public void f(int n) {

?? while(k <= n) {

??? if(k == n) {

??? ?System.out.println(n);

???? break;

??? } else if(n > k && n % k== 0) {

???? System.out.print(k + "*");

???? n = n / k;

???? f(n);

???? break;

??? } else if(n > k && n % k!= 0) {

???? k++;

???? f(n);

???? break;

??? }

?? }

}

}

/*

【程序5】作者 南楓題目:利用條件運算符的嵌套來完成此題:學(xué)習(xí)成績>=90分的同學(xué)用A表示,60-89分之間的用B表示,60分以下的用C表示。

1.

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

package cn.com.flywater.FiftyAlgorthm;

import java.util.Scanner;

public class FifthCondition {

//public static final int S1 = 90;

//public static final int S2 = 60;

static int grade;

public static void main(String[] args) {

?? Scanner str = new Scanner(System.in);

?? int s = str.nextInt();

?? FifthCondition fc = newFifthCondition();

?? grade = fc.compare(s);

?? if(grade == 1) {

??? System.out.print('A');

?? } else if(grade == 2) {

??? System.out.print('B');

?? } else {

??? System.out.println('C');

?? }

}

public int compare(int s) {

?? return s > 90 ? 1

???? : s > 60 ? 2

???? :3;

}

}

/*

【程序6】作者 南楓題目:輸入兩個正整數(shù)m和n,求其最大公約數(shù)和最小公倍數(shù)。

1.

程序分析:利用輾除法。*/

/*

*

在循環(huán)中,只要除數(shù)不等于0,用較大數(shù)除以較小的數(shù),將小的一個數(shù)作為下一輪循環(huán)的大數(shù),取得的余數(shù)作為下一輪循環(huán)的較小的數(shù),如此循環(huán)直到較小的數(shù)的值為0,返回

*

較大的數(shù),此數(shù)即為最小公約數(shù),最小公倍數(shù)為兩數(shù)之積除以最小公倍數(shù)。

* */

package cn.com.flywater.FiftyAlgorthm;

import java.util.Scanner;

public class SixthCommonDiviser {

public static void main(String[] args) {

?? int a, b;

?? Scanner s1 = new Scanner(System.in);

?? Scanner s2 = new Scanner(System.in);

?? a = s1.nextInt();

?? b = s2.nextInt();

?? SixthCommonDiviser scd = newSixthCommonDiviser();

?? int m = scd.division(a, b);

?? int n = a * b / m;

?? System.out.println("

最大公約數(shù):" + m);

?? System.out.println("

最小公倍數(shù):" + n);

}

publicint division(int x, int y) {

?? int t;

?? if(x < y) {

??? t = x;

??? x = y;

??? y = t;

?? }

?? while(y != 0) {

??? if(x == y) return 1;

??? else {

???? int k = x % y;

???? x = y;

???? y = k;

??? }

?? }

? ?return x;

}

}

/*【程序7】作者 南楓題目:輸入一行字符,分別統(tǒng)計出其中英文字母、空格、數(shù)字和其它字符的個數(shù)。

1.

程序分析:利用while語句,條件為輸入的字符不為'\n '. */

packagecn.com.flywater.FiftyAlgorthm;

import java.util.*;

public class SeventhCharacterStatistics {

static int digital = 0;

static int character = 0;

static int other = 0;

static int blank = 0;

public static void main(String[] args) {

?? char[] ch = null;

?? Scanner sc = new Scanner(System.in);

?? String s = sc.nextLine();

?? ch = s.toCharArray();

?? for(int i=0; i

??? if(ch[i] >= '0' && ch[i]<= '9') {

???? digital ++;

??? } else if((ch[i] >= 'a' &&ch[i] <= 'z') || ch[i] > 'A' && ch[i] <= 'Z') {

???? character ++;

??? } else if(ch[i] == ' ') {

???? blank ++;

??? } else {

???? other ++;

??? }

?? }

?? System.out.println("數(shù)字個數(shù):" + digital);

?? System.out.println("

英文字母個數(shù):" + character);

?? System.out.println("

空格個數(shù):" + blank);

?? System.out.println("

其他字符個數(shù):"+ other );

}

}

/*【程序8】作者 南楓題目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數(shù)字。例如2+22+222+2222+22222(此時共有5個數(shù)相加),幾個數(shù)相加有鍵盤控制。

*/

/*

*

算法: 定義一個變量b,賦初值為0;定義一變量sum,賦初值為0,

*

進(jìn)入循環(huán)后,將a + b 的值賦給b,將sum

+ b 的值賦給sum;

*

同時,將a 增加十倍,++ i; 繼續(xù)循環(huán);

*

循環(huán)結(jié)束后,輸出sum 的值。

*/

package cn.com.flywater.FiftyAlgorthm;

import java.util.Scanner;

public class EightPlus {

static long a = 2, b = 0;

public static void main(String[] args) {

?? Scanner s = new Scanner(System.in);

?? int n = s.nextInt();

?? int i = 0;

?? long sum = 0;

?? while(i < n) {

??? b = b + a;

??? sum = sum + b;

??? a = a * 10;

??? ++ i;

?? }

?? System.out.println("input number:" + n);

?? System.out.println(sum);

}

}

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

*/

package cn.com.flywater.FiftyAlgorthm;

public class NinthWanshu {

publicstatic void main(String[] args) {

?? System.out.println("1

到1000的完數(shù)有:");

?? 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次落地時,共經(jīng)過多少米?第10次反彈多高?

*/

package cn.com.flywater.FiftyAlgorthm;

public class TenthTreeFall {

static double height = 100;

static double distance = 100;

public static void main(String[] args) {

?? for(int i=1; i<10; i++) {

??? distance = distance + height;

??? height = height / 2;

?? }

?? System.out.println("

路程:"+ distance);

?? System.out.println("

高度:"+ height / 2);

}

}

/*

【程序11】

*

作者 南楓題目:有1、2、3、4個數(shù)字,能組成多少個互不相同且無重復(fù)數(shù)字的三位數(shù)?都是多少?

1.

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

*/

/*

算法:3個for循環(huán)加一個if語句;

*

*/

package cn.com.flywater.FiftyAlgorthm;

public class EleventhNumberRange {

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.print(x*100 + y*10 + z+ "?? ");

?????? if(count % 4 == 0) {

??????? System.out.println();

?????? }

????? }

???? }

??? }

?? }

?? System.out.println("

共有"

+ count + "個三位數(shù)");

}

}

/*【程序12】

*

作者 南楓題目:企業(yè)發(fā)放的獎金根據(jù)利潤提成。利潤(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%提成,從鍵盤輸入當(dāng)月利潤I,求應(yīng)發(fā)放獎金總數(shù)?

1.

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

*/

/*

注意: 要精確到小數(shù)點后多少位,用DecimalFormat df = new DecimalFormat("#0.0000");

*

*/

package cn.com.flywater.FiftyAlgorthm;

import java.text.DecimalFormat;

import java.util.*;

public class TwelfthProfitAward {

static double profit = 0;

static double award = 0;

public static void main(String[] args) {

?? Scanner s = new Scanner(System.in);

?? profit = s.nextInt();

?? System.out.println("

輸入的利潤是"

+ profit + "萬");

?? if(profit > 0 && profit<= 10) {

??? award = profit * 0.1;

?? } else if(profit > 10 &&profit <= 20) {

??? award = 10 * 0.1 + (profit - 10) *0.075;

?? } else if(profit > 20 &&profit <= 40) {

??? award = 10 * 0.1 + 10 * 0.075 +(profit - 20) * 0.05;

?? } else if(profit > 40 &&profit <= 60) {

??? award = 10 * 0.1 + 10 * 0.075 + 20 *0.05 + (profit - 40) * 0.03;

?? } else if(profit > 60 &&profit <= 100) {

??? award = 20 * 0.175 + 20 * 0.05 + 20 *0.03 + (profit - 60) * 0.015;

?? } else if(profit > 100) {

??? award = 20 * 0.175 + 40 * 0.08 + 40 *0.015 + (profit - 100) * 0.01;

?? }

?? DecimalFormat df = newDecimalFormat("#0.00000");

?? System.out.println("

應(yīng)該提取的獎金是" + df.format(award) + "萬");

}

}

/*【程序13】

*

作者 南楓題目:一個整數(shù),它加上100后是一個完全平方數(shù),再加上168又是一個完全平方數(shù),請問該數(shù)是多少?

1.

程序分析:在10萬以內(nèi)判斷,先將該數(shù)加上100后再開方,再將該數(shù)加上268后再開方,如果開方后的結(jié)果滿足如下條件,即是結(jié)果。請看具體分析:

*/

package cn.com.flywater.FiftyAlgorthm;

public class ThirteenthTwiceSqrt {

public static void main(String[] args) {

?? for(long l=1L; l<100000; l++) {

??? if(Math.sqrt((long)(l+100)) % 1 == 0){

???? if(Math.sqrt((long)(l+268)) % 1 ==0) {

????? System.out.println(l + "

加100是一個完全平方數(shù),再加168又是一個完全平方數(shù)");

???? }

??? }

?? }

}

}

*【程序14】

*

作者 南楓題目:輸入某年某月某日,判斷這一天是這一年的第幾天?

1.

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

*/

package cn.com.flywater.FiftyAlgorthm;

import java.util.Scanner;

import java.io.*;

public class FourteenthYearMonthDay {

public static void main(String[] args) {

?? int year, month, day;

?? int days = 0;

?? int d = 0;

?? FourteenthYearMonthDay fymd = newFourteenthYearMonthDay();

?? System.out.print("Input theyear:");

?? year =fymd.input();

?? System.out.print("Input themonth:");

?? month = fymd.input();

?? System.out.print("Input TheDay:");

?? day = fymd.input();

?? if (year < 0 || month < 0 ||month > 12 || day < 0 || day > 31) {

??? System.out.println("Input error,please run this program again!");

??? System.exit(0);

?? }

?? for (int i=1; i

??? switch (i) {

??? case 1:

??? case 3:

??? case 5:

??? case 7:

??? case 8:

??? case 10:

??? case 12:

???? days = 31;

???? //d += days;

???? break;

??? case 4:

??? case 6:

??? case 9:

??? case 11:

???? days = 30;

???? //d += days;

???? break;

??? case 2:

???? if ((year % 400 == 0) || (year % 4== 0 && year % 100 != 0)) {

????? days = 29;

???? } else {

????? days = 28;

???? }

???? //d += days;

???? break;

??? }

??? d += days;

?? }

?? System.out.println(year +":" + month + ":" + day + "

是今年的第"

+ (d+day) + "天。");

}

public int input() {

?? int value = 0;

?? Scanner s = new Scanner(System.in);

?? value = s.nextInt();

?? return value;

}

}

/*【程序15】

*

作者??南楓題目:輸入三個整數(shù)x,y,z,請把這三個數(shù)由小到大輸出。

1.

程序分析:我們想辦法把最小的數(shù)放到x上,先將x與y進(jìn)行比較,如果x>

y則將x與y的值進(jìn)行交換,然后再用x與z進(jìn)行比較,如果x>

z則將x與z的值進(jìn)行交換,這樣能使x最小。

*/

package cn.com.flywater.FiftyAlgorthm;

import java.util.*;

public class FifteenthNumberCompare {

public static void main(String[] args) {

?? FifteenthNumberCompare fnc = newFifteenthNumberCompare();

?? int a, b, c;

?? System.out.println("Input 3numbers:");

?? a = fnc.input();

?? b = fnc.input();

?? c = fnc.input();

//

//?? fnc.compare(a, b);//

方法調(diào)用不能通過改變形參的值來改變實參的值

//?? fnc.compare(b, c);//

這種做法是錯的

//?? fnc.compare(a, c);

?? //System.out.println("result:"+ a +" " + b + " " + c);//

沒有改變

?? if(a > b) {

??? int t = a;

??? a = b;

??? b = t;

?? }

?? if(a > c) {

??? int t = a;

??? a = c;

??? c = t;

?? }

?? if(b > c) {

??? int t = b;

??? b = c;

??? c = t;

?? }

?? System.out.println( a + " "+ b + " " + c);

}

public int input() {

?? int value = 0;

?? Scanner s = new Scanner(System.in);

?? value = s.nextInt();

?? return value;

}

public void compare(int x, int y) {//

此方法沒用

?? if(x > y) {

??? int t = x;

??? x = y;

??? y = t;

?? }

}

}

/*【程序16】

*

作者 南楓

*

題目:輸出9*9口訣。

*1.

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

**/

package cn.com.flywater.FiftyAlgorthm;

public class SixteenthMultiplicationTable {

public static void main(String[] args) {

?? for(int i=1; i<10; i++) {

??? for(int j=1; j<=i; j++) {

???? System.out.print(j + "*" +i + "=" + j*i + " " );

??? }

??? System.out.println();

?? }

}

}

//

【程序17】

//

作者 南楓

//

題目:猴子吃桃問題:猴子第一天摘下若干個桃子,當(dāng)即吃了一半,還不癮,

//

又多吃了一個 第二天早上又將剩下的桃子吃掉一半,又多吃了一個。

//

以后每天早上都吃了前一天剩下 的一半零一個。到第10天早上想再吃時,

//

見只剩下一個桃子了。求第一天共摘了多少。

//1.

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

packagecn.com.flywater.FiftyAlgorthm;

public class SeventeenthMonkeyPeach {

public static void main(String[] args) {

?? int lastdayNum = 1;

?? for(int i=2; i<=10; i++) {

??? lastdayNum = (lastdayNum+1) * 2;

?? }

?? System.out.println("

猴子第一天摘了" + lastdayNum + " 個桃子");

}

}

/*

【程序18】

*

作者 南楓題目:兩個乒乓球隊進(jìn)行比賽,各出三人。甲隊為a,b,c三人,乙隊為x,y,z三人。已抽簽決定比賽名單。有人向隊員打聽比賽的名單。a說他不和x比,c說他不和x,z比,請編程序找出三隊賽手的名單。

*/

/*

*

這個程序?qū)懙煤懿缓?,是知道結(jié)果后拼湊起來的,還不如直接寫輸出語句加上結(jié)果來的好。

*/

package cn.com.flywater.FiftyAlgorthm;

public class EighteenthPingpong {

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]);

??? }

?? }

}

}

題目:打印出如下圖案(菱形)

?? *

? ***

?*****

*******

?*****

? ***

?? *

1.

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

*/

/*

上半部分循環(huán)變量的控制方法是

* for(int i=0; i<(HEIGHT+1) / 2; i++) {

?? for(int j=1; j

?? for(int k=1; k<(i+1)*2; k++) {

下半部分循環(huán)變量的控制方法是

for(int i=1; i<=HEIGHT/2; i++) {

?? for(int j=1; j<=i; j++) {

*??? for(int k=1; k<=WIDTH-2*i-1; k++){

*/

package cn.com.flywater.FiftyAlgorthm;

public class NineteenthPrintRhombic {

static final int HEIGHT = 7;

static final int WIDTH = 8;

public static void main(String[] args) {

?? for(int i=0; i<(HEIGHT+1) / 2; i++){

??? for(int j=1; j

???? System.out.print(" ");

??? }

??? for(int k=1; k<(i+1)*2; k++) {

???? System.out.print('*');

??? }

??? System.out.println();

?? }

?? for(int i=1; i<=HEIGHT/2; i++) {

??? for(int j=1; j<=i; j++) {

???? System.out.print(" ");

??? }

??? for(int k=1; k<=WIDTH-2*i-1; k++){

???? System.out.print('*');

??? }

??? System.out.println();

?? }

}

}

上半部分第二重循環(huán)應(yīng)改為:??? for(int j=0; j

上半部分第三重循環(huán)應(yīng)改為:??? for(int k=1; k<=WIDTH-2*i; k++)

/*

【程序20】

*

作者 南楓題目:有一分?jǐn)?shù)序列:2/1,3/2,5/3,8/5,13/8,21/13...求出這個數(shù)列的前20項之和。

1.

程序分析:請抓住分子與分母的變化規(guī)律。

*/

packagecn.com.flywater.FiftyAlgorthm;

import java.text.DecimalFormat;

public class TwentiethFractionSum {

public static void main(String[] args) {

?? int x = 2, y = 1, t;

?? double sum = 0;

?? DecimalFormat df = newDecimalFormat("#0.0000");

?? for(int i=1; i<=20; i++) {

??? sum += (double)x / y;

??? t = y;

??? y = x;

??? x = y + t;

??? System.out.println("

第" + i + " 次相加,和是" + df.format(sum));

?? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容