JAVA經(jīng)典算法面試10題及答案_第1頁(yè)
JAVA經(jīng)典算法面試10題及答案_第2頁(yè)
JAVA經(jīng)典算法面試10題及答案_第3頁(yè)
JAVA經(jīng)典算法面試10題及答案_第4頁(yè)
JAVA經(jīng)典算法面試10題及答案_第5頁(yè)
免費(fèi)預(yù)覽已結(jié)束,剩余1頁(yè)可下載查看

付費(fèi)下載

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、JAVA經(jīng)典算法面試10題及答案1 .程序分析:兔子的規(guī)律為數(shù)列1,1,2,3,5,8,13,21 .public class exp2 public static void main(string args) inti=0; for(i=1;i i+) system.out.println(f(i); public static int f(intx) 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 mym

2、ath = new math(); for(i=1;ii+)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ù)。1 .程序分析:判斷素?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) inti=

3、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(intx) for(int i=2;i=x/2;i+) if (x % 2=0 ) return false; return true; 【程序 3】題目:打印出所有的 水仙花數(shù),所謂 水仙

4、花數(shù) 是指一個(gè)三位數(shù),其 各位數(shù)字立方和等于該數(shù)本身。例如:153是一個(gè)水仙花數(shù),因?yàn)?153=1的三次方+ 5的三次方+ 3的三次方。1 .程序分析:利用for循環(huán)控制100-999個(gè)數(shù),每個(gè)數(shù)分解出個(gè) 位,十位,百位。public class exp2 public static void main(string args) inti=0; math mymath = new math(); for(i=100;i=999;i+)if(mymath.shuixianhua(i)=true) system.out.println(i); classmath public int f(int

5、x) if(x=1 | x=2) return 1; else returnf(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】題目:

6、將一個(gè)正整數(shù)分解質(zhì)因數(shù)。例如: 輸入90,打印出90=2*3*3*5 。程序分析:對(duì)n進(jìn)行分解質(zhì)因數(shù),應(yīng)先找到一個(gè)最小的質(zhì)數(shù)k,然后按下述步驟完成:(1)如果這個(gè)質(zhì)數(shù)恰等于n,則說(shuō)明分解質(zhì)因數(shù)的過(guò)程已經(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(intn) for(int i=2;i =n/2;i+)if(n%i=0) system.out.pri

7、nt(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( 請(qǐng)輸入 n 的值( 輸 入 exit 退 出):int n; n=0;try n=integer.parseint(str); catch(numberformatexception e) e.printstacktrace(

8、); system.out.print(n+ 分解質(zhì)因數(shù): +n+ =c.fengjie(n); 【程序5】題目:利用條件運(yùn)算符的嵌套來(lái)完成此 題:學(xué)習(xí)成績(jī)=90分的同學(xué)用a表示,60-89分之間的用b表示, 60分以下的用c表示。1.程序分析:(a b)?a:b這是條件運(yùn)算符的基本例子。import javax.swing.*;public class ex5 public static void main(stringargs)stringstr=str=joptionpane.showinputdialog( 請(qǐng)輸入 n 的值(輸入 exit 退 出): int n; n=0; try

9、n=integer.parseint(str); catch(numberformatexception e) e.printstacktrace(); str=(n 90? a :(n 60? b : c ); system.out.println(str); 【程序6】題目:輸入兩個(gè)正整數(shù)m和n,求其最大公約數(shù)和最 小公倍數(shù)。1.程序分析:利用輾除法。最大公約數(shù): public class commondivisor public static voidmain(string args) commondivisor(24,32); static intcommondivisor(int m

10、, int n) if(n 011m0) system.out.println( error! return -1;if(n=0) system.out.println( the biggest common divisor is : +m); return m; return commondivisor(n,m%n); 最小公倍數(shù)和最大公約數(shù):import java.util.scanner; public class candc /下面的方法是求出最大公約數(shù) public static int gcd(int m, int n) while (true) if (m = m % n) =

11、0) return n; if (n = n % m) = 0) return m; public static void main(string args) throws exception / 取得 輸入值 /scanner chin = new scanner(system.in); /int a = chin.nextint(), b = chin.nextint();int a=23; int b=32;int c = gcd(a, b); system.out.println(最小公倍數(shù):+ a * b / c + n 最大公約數(shù):+ c); 【程序7】題目:輸 入一行字符,分別統(tǒng)

12、計(jì)出其中英文字母、空格、數(shù)字和其它字符的個(gè) 數(shù)。1.程序分析:利用while語(yǔ)句,條件為輸入的字符不為n .import java.util.scanner;public class ex7 public static void main(string args) system.out.println( 請(qǐng)輸入 字符串: scanner scan=new scanner(system.in); string str=scan.next(); string e1= u4e00-u9fa5 string e2= a-za-z int counth=0; int counte=0; char arr

13、char=str.tochararray(); string arrstr=new stringarrchar.length; for (int i=0;i4arrchar.length ;i+ ) arrstri=string.valueof(arrchari); for(string i: arrstr ) if (i.matches(e1) counth+; if (i.matches(e2) counte+; system.out.println(漢字的個(gè)數(shù)+counth); system.out.println( 字母的個(gè)數(shù) +counte); 【程序 8題目:求s=a+aa+aaa

14、+aaaa+aa a 的值,其中a是一個(gè)數(shù)字。 例如2+22+222+2222+22222( 此時(shí)共有5個(gè)數(shù)相加),幾個(gè)數(shù)相加 有鍵盤(pán)控制。1.程序分析:關(guān)鍵是計(jì)算出每一項(xiàng)的值。import java.io.*;public class sumloop public static void main(string args) throws ioexception int s=0; string output= bufferedreader stadin = new bufferedreader(new inputstreamreader(system.in); system.out.print

15、ln( 請(qǐng)輸入 a 的 值 string input =stadin.readline(); for(int i =1;i =integer.parseint(input);i+) output+=input;inta=integer.parseint(output); s+=a; system.out.println(s); 另 解:import java.io.*;public class sumloop public static void main(string args) throws ioexception int s=0; int n; int t=0; bufferedread

16、er stadin = new bufferedreader(new inputstreamreader(system.in); string input = stadin.readline(); n=integer.parseint(input); for(int i=1;i i+) t=t*10+n; s=s+t; system.out.println(t); system.out.println(s); 【程序 9】題目: 一個(gè)數(shù)如果恰好等于它的因子之和,這個(gè)數(shù)就稱(chēng)為 完數(shù)。例如6=1+ 2 + 3.編程 找出1000以內(nèi)的所有完數(shù)。public class wanshu public static void main(string args) int s; for(int i=1;i =1000;i+) s=0; for(int j=1;j j+) if(i %j=0) s=s+j; if(s=i) system.out.p

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論