更新時(shí)間:2021-06-01 12:18:24 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1029次
題目1:古典問題:有一對兔子,從出生后第3個(gè)月起每個(gè)月都生一對兔子,小兔子長到第四個(gè)月后每個(gè)月又生一對兔子,假如兔子都不死,問每個(gè)月的兔子總數(shù)為多少?
程序分析: 兔子的規(guī)律為數(shù)列1,1,2,3,5,8,13,21….
public class exp2{
public static void main(String args[]){
int i=0;
for(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 exp2{
public static void main(String args[]){
int i=0;
math mymath = new math();
for(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之間有多少個(gè)素?cái)?shù),并輸出所有素?cái)?shù)。
程序分析:判斷素?cái)?shù)的方法:用一個(gè)數(shù)分別去除2到sqrt(這個(gè)數(shù)),如果能被整除,
則表明此數(shù)不是素?cái)?shù),反之是素?cái)?shù)。
public class exp2{
public static void main(String args[]){
int i=0;
math mymath = new math();
for(i=2;i<=200;i++)
if(mymath.iszhishu(i)==true)
System.out.println(i);
}
}
class math
{
public int f(int x)
{
if(x==1 || x==2)
return 1;
else
return f(x-1)+f(x-2);
}
public boolean iszhishu(int x)
{
for(int i=2;i<=x/2;i++)
if (x % 2==0 )
return false;
return true;
}
}
題目3:打印出所有的 “水仙花數(shù) “,所謂 “水仙花數(shù) “是指一個(gè)三位數(shù),其各位數(shù)字立方和等于該數(shù)本身。例如:153是一個(gè) “水仙花數(shù) “,因?yàn)?53=1的三次方+5的三次方+3的三次方。
程序分析:利用for循環(huán)控制100-999個(gè)數(shù),每個(gè)數(shù)分解出個(gè)位,十位,百位。
public class exp2{
public static void main(String args[]){
int i=0;
math mymath = new math();
for(i=100;i<=999;i++)
if(mymath.shuixianhua(i)==true)
System.out.println(i);
}
}
class math
{
public int f(int x)
{
if(x==1 || x==2)
return 1;
else
return f(x-1)+f(x-2);
}
public boolean iszhishu(int x)
{
for(int i=2;i<=x/2;i++)
if (x % 2==0 )
return false;
return true;
}
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:將一個(gè)正整數(shù)分解質(zhì)因數(shù)。例如:輸入90,打印出90=2*3*3*5。
程序分析:對n進(jìn)行分解質(zhì)因數(shù),應(yīng)先找到一個(gè)最小的質(zhì)數(shù)k,然后按下述步驟完成:
(1)如果這個(gè)質(zhì)數(shù)恰等于n,則說明分解質(zhì)因數(shù)的過程已經(jīng)結(jié)束,打印出即可。
(2)如果n <> k,但n能被k整除,則應(yīng)打印出k的值,并用n除以k的商,作為新的正整數(shù)你,重復(fù)執(zhí)行第一步。
(3)如果n不能被k整除,則用k+1作為k的值,重復(fù)執(zhí)行第一步。
public class exp2{
public exp2(){}
public void fengjie(int n){
for(int i=2;i<=n/2;i++){
if(n%i==0){
System.out.print(i+"*");
fengjie(n/i);
}
}
System.out.print(n);
System.exit(0);///不能少這句,否則結(jié)果會(huì)出錯(cuò)
}
public static void main(String[] args){
String str="";
exp2 c=new exp2();
str=javax.swing.JOptionPane.showInputDialog("請輸入N的值(輸入exit退出):");
int N;
N=0;
try{
N=Integer.parseInt(str);
}catch(NumberFormatException e){
e.printStackTrace();
}
System.out.print(N+"分解質(zhì)因數(shù):"+N+"=");
c.fengjie(N);
}
}
題目5:利用條件運(yùn)算符的嵌套來完成此題:學(xué)習(xí)成績> =90分的同學(xué)用A表示,60-89分之間的用B表示,60分以下的用C表示。
程序分析:(a> b)?a:b這是條件運(yùn)算符的基本例子。
import javax.swing.*;
public class ex5 {
public static void main(String[] args){
String str="";
str=JOptionPane.showInputDialog("請輸入N的值(輸入exit退出):");
int N;
N=0;
try{
N=Integer.parseInt(str);
}
catch(NumberFormatException e){
e.printStackTrace();
}
str=(N>90?"A":(N>60?"B":"C"));
System.out.println(str);
}
}
以上就是動(dòng)力節(jié)點(diǎn)小編介紹的"Java算法面試題解答",希望對大家有幫助,如有疑問,請?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為您服務(wù)。

初級 202925

初級 203221

初級 202629

初級 203743