25道經典Java算法題

即使做web開發,也會遇到各種各種需要解決的算法問題,本文節選部分經典練手算法,並提供相關參考答案,希望對你有所幫助

【程序1】

題目:古典問題:有一對兔子,從出生後第3個月起每個月都生一對兔子,小兔子長到第三個月後每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數為多少? //這是一個菲波拉契數列問題

public class test01 {

public static void main(String[] args) {

int f1=1,f2=1,f;

int M=30;

System.out.println(1);

System.out.println(2);

for(int i=3;i

f=f2;

f2=f1+f2;

f1=f;

System.out.println(f2);

}

}

}

【程序2】

題目:判斷101-200之間有多少個素數,並輸出所有素數。 程序分析:判斷素數的方法:用一個數分別去除2到sqrt(這個數),如果能被整除, 則表明此數不是素數,反之是素數。

public class test02 {

public static void main(String[] args) {

int count=0;

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

boolean flag=true;

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

if(i%j==0) {

flag=false;

break;

}

}

if(flag==true) {

count++;

System.out.println(i);

}

}

System.out.println(count);

}

}

【程序3】

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

public class test03 {

public static void main(String[] args) {

int a,b,c;

for(int i=101;i<1000;i++) {

a=i%10;

b=i/10%10;

c=i/100;

if(a*a*a+b*b*b+c*c*c==i)

System.out.println(i);

}

}

}

【程序4】

題目:將一個正整數分解質因數。例如:輸入90,打印出90=233*5。 程序分析:對n進行分解質因數,應先找到一個最小的質數k,然後按下述步驟完成: (1)如果這個質數恰等於n,則說明分解質因數的過程已經結束,打印出即可。 (2)如果n <> k,但n能被k整除,則應打印出k的值,並用n除以k的商,作為新的正整數你n,重複執行第一步。 (3)如果n不能被k整除,則用k+1作為k的值,重複執行第一步。

import java.util.Scanner;

public class test04 {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

int n=input.nextInt();

int k=2;

while(n>=k) {

if(n==k) {

System.out.println(k);

break;

}else if (n%k==0) {

System.out.println(k);

n=n/k;

}else {

k++;

}

}

}

}

【程序5】

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

import java.util.Scanner;public class test05 {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

int score=input.nextInt();

char grade=score>=90?'A':score>=60?'B':'C';

System.out.println(grade);

}

}

【程序6】

題目:輸入兩個正整數m和n,求其最大公約數和最小公倍數。 /*在循環中,只要除數不等於0,用較大數除以較小的數,將小的一個數作為下一輪循環的大數,取得的餘數作為下一輪循環的較小的數,如此循環直到較小的數的值為0,返回較大的數,此數即為最大公約數,最小公倍數為兩數之積除以最大公約數。 /

import java.util.Scanner;public class test06 {

public static void main(String[] args) {

Scanner input =new Scanner(System.in);

int a=input.nextInt();

int b=input.nextInt();

test06 test=new test06();

int i = test.gongyinshu(a, b);

System.out.println("最小公因數"+i);

System.out.println("最大公倍數"+a*b/i);

}

public int gongyinshu(int a,int b) {

if(a

int t=b;

b=a;

a=t;

}

while(b!=0) {

if(a==b)

return a;

int x=b;

b=a%b;

a=x;

}

return a;

}

}

【程序7】

題目:輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數。

import java.util.Scanner;

public class test07 {

public static void main(String[] args) {

int abccount=0;

int spacecount=0;

int numcount=0;

int othercount=0;

Scanner input=new Scanner(System.in);

String toString=input.nextLine();

char [] ch=toString.toCharArray();

for(int i=0;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(spacecount);

System.out.println(numcount);

System.out.println(othercount);

}

}

【程序8】

題目:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一個數字。例如2+22+222+2222+22222(此時共有5個數相加),幾個數相加有鍵盤控制。

import java.util.Scanner;

public class test08 {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

int a=input.nextInt();

int n=input.nextInt();

int sum=0,b=0;

for(int i=0;i

b+=a;

sum+=b;

a=a*10;

}

System.out.println(sum);

}

}

【程序9】

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

public class test09 {

public static void main(String[] args) {

for(int i=1;i<=1000;i++) {

int t = 0;

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

if(i%j==0) {

t+=j;

}

}

if(t==i) {

System.out.println(i);

}

}

}

}

【程序10】

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

public class test10 {

public static void main(String[] args) {

double h=100;

double s=100;

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

h=h/2;

s=s+2*h;

}

System.out.println(s);

System.out.println(h);

}

}

【程序11】

題目:有1、2、3、4四個數字,能組成多少個互不相同且一個數字中無重複數字的三位數?並把他們都輸入。 public

class test11 {

public static void main(String[] args) {

int count=0;

for(int i=1;i<5;i++) {

for(int j=1;j<5;j++) {

for(int k=1;k<5;k++) {

if(i!=j&&j!=k&&i!=k) {

count++;

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%提成,從鍵盤輸入當月利潤,求應發放獎金總數? import java.util.Scanner;public class

test12 {

public static void main(String[] args) {

Scanner input =new Scanner(System.in);

double x=input.nextDouble();

double y=0;

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=10*0.1+10*0.075+20*0.05+20*0.03+(x-60)*0.015;

}else if (x>100) {

y=10*0.1+10*0.075+20*0.05+20*0.03+40*0.015+(x-100)*0.01;

}

System.out.println(y);

}

}

【程序13】

題目:一個整數,它加上100後是一個完全平方數,再加上168又是一個完全平方數,請問該數是多少?

public class test13 {

public static void main(String[] args) {

for(int i=-100;i<10000;i++) {

if(Math.sqrt(i+100)%1==0&&Math.sqrt(i+268)%1==0) {

System.out.println(i);

}

}

}

}

【程序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

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.Scanner;

public class test15 {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

int x=input.nextInt();

int y=input.nextInt();

int z=input.nextInt();

int t=0;

if(x>y) {

t=x;

x=y;

y=t;

}

if(y>z) {

t=z;

z=y;

y=t;

}

if(x>y) {

t=x;

x=y;

y=t;

}

System.out.println(x+""+y+""+z);

}

}

【程序16】

題目:輸出9*9口訣。

public class test16 {

public static void main(String[] args) {

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

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

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

System.out.print(" ");

}

System.out.println("");

}

}

}

【程序17】

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

public class test17 {

public static void main(String[] args) {

int x=1;

for(int i=10;i>1;i--) {

x=(x+1)*2;

}

System.out.println(x);

}

}

【程序18】

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

public class test18 {

public static void main(String[] args) {

for(char i='x';i<='z';i++) {

for (char j='x';j<='z';j++) {

if(i!=j) {

for(char k='x';k<='z';k++) {

if(i!=k&&j!=k) {

if(i!='x'&&j!='x'&&j!='z') {

System.out.println("a:"+i+"\nb:"+j+"\nc:"+k);

}

}

}

}

}

}

}}

【程序19】

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

public class lianxi19 {

public static void main(String[] args) {

int H = 7, W = 7;//高和寬必須是相等的奇數

for(int i=0; i

for(int j=0; j

System.out.print(" ");

}

for(int k=1; k

System.out.print('*');

}

System.out.println();

}

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

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

System.out.print(" ");

}

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

System.out.print('*');

}

System.out.println();

}

}

}

【程序20】

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

public class test20 {

public static void main(String[] args) {

double sum=0,ver=2;

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

sum+=ver/i;

ver+=i;

}

System.out.println(sum);

}

}

【程序21】

題目:求1+2!+3!+…+20!的和

public class test21 {

public static void main(String[] args) {

long sum=0,ver=1;

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

ver=ver*i;

sum+=ver;

}

System.out.println(sum);

}}

【程序22】

題目:利用遞歸方法求5!。

public class test22 {

public static void main(String[] args) {

System.out.println(fac(5));

}

public static int fac(int i) {

if(i==1) return 1;

else {

return i*fac(i-1);

}

}

}

【程序23】

題目:有5個人坐在一起,問第五個人多少歲?他說比第4個人大2歲。問第4個人歲數,他說比第3個人大2歲。問第三個人,又說比第2人大兩歲。問第2個人,說比第一個人大兩歲。最後問第一個人,他說是10歲。請問第五個人多大?

public class test23 {

public static void main(String[] args) {

int age=10;

for(int i=2;i<=5;i++) {

age+=2;

}

System.out.println( age);

}

}

【程序24】

題目:給一個不多於5位的正整數,要求:一、求它是幾位數,二、逆序打印出各位數字。 //使用了長整型最多輸入18位

import java.util.Scanner;

public class test24 {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

String toString=input.nextLine();

char[] num=toString.toCharArray();

System.out.println(num.length);

for(int i=num.length;i>0;i--) {

System.out.print(num[i-1]);

}

}

}

【程序25】

題目:一個5位數,判斷它是不是迴文數。即12321是迴文數,個位與萬位相同,十位與千位相同。

import java.util.Scanner;public class test25 {

public static void main(String[] args) {

Scanner input =new Scanner(System.in);

int numtest=input.nextInt();

System.out.println(ver(numtest));

}

public static boolean ver(int num) {

if(num<0||(num!=0&&num%10==0))

return false;

int ver=0;

while(num>ver) {

ver=ver*10+num%10;

num=num/10;

}

return(num==ver||num==ver/10);

}

}


分享到:


相關文章: