JAVA经典算法面试10题及答案【精选】

JAVA经典算法面试10题及答案

【程序1】题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21….public class exp2{public static void main{int i=0;for);}public static int f{ifreturn 1;elsereturn f+f;}}或public class exp2{public static void main{int i=0;math mymath = new math;for);}}class math{public int f{ifreturn 1;elsereturn f+f;}}

【程序2】题目:判断101-200之间有多少个素数,并输出所有素数。 1.程序分析:判断素数的方法:用一个数分别去除2到sqrt,如果能被整除,

则表明此数不是素数,反之是素数。public class exp2{public static void main{int i=0;math mymath = new math;forif==true);}}class math{public int f{ifreturn 1;elsereturn f+f;}public boolean iszhishu{forif return false;return true;}}

【程序3】题目:打印出所有的 “水仙花数 “,所谓 “水仙花数 “是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 “水仙花数 “,因为153=1的三次方+5的三次方+3的三次方。

1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。public class exp2{ public static void main{ int i=0; math mymath = new math; for if==true) ; }}class math{ public int f { if return 1; else return f+f; } public boolean iszhishu { for if return false; return true; } public boolean shuixianhua { int i=0,j=0,k=0; i=x / 100; j= /10; k=x % 10; if return true; else return false; }}

【程序4】题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:

如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。 如果n k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为

新的正整数你,重复执行第一步。

如果n不能被k整除,则用k+1作为k的值,重复执行第一步。public class exp2{public exp2{} public void fengjie{ for{ if{ ; fengjie; } } ; ;s:///不能少这句,否则结果会出错 } public static void main{ string str=““; exp2 c=new exp2;

str=;

int n;

n=0;

try{ n=; }catch{ ; } ; ; } }

【程序5】题目:利用条件运算符的嵌套来完成此题:学习成绩> =90分的同学用a表示,60-89分之间的用b表示,60分以下的用c表示。

1.程序分析:?a:b这是条件运算符的基本例子。import *;public class ex5

{ public static void main{ string str=““; str=; int n; n=0; try{ n=; } catch{ ; } str=); ; } }

【程序6】题目:输入两个正整数m和n,求其最大公约数和最小公倍数。 1.程序分析:利用辗除法。最大公约数:public class mondivisor{ public static void main { mondivisor; } static int mondivisor { if { ; return -1; } if { ; return m; } return mondivisor; }}最小公倍数和最大公约数:import ; public class candc { s://下面的方法是求出最大公约数public static int gcd { while { if == 0) return n; if == 0) return m; } } public static void main throws exception { s://取得输入值s://scanner chin = new scanner; s://int a = , b = ; int a=23; int b=32;int c = gcd; ; } }

【程序7】题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

1.程序分析:利用while语句,条件为输入的字符不为 ‘\\n ‘.import ;public class ex7 { public static void main { ; scanner scan=new scanner; string str=; string e1=“[\一-\龥]”; string e2=“[a-za-z]”; int counth=0; int counte=0; char[] arrchar=; string[] arrstr=new string[]; for { arrstr[i]=; } for { if ) { counth++; } if ) { counte++; } } ; ; }}

【程序8】题目:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例

如2+22+222+2222+22222,几个数相加有键盘控制。

1.程序分析:关键是计算出每一项的值。import *;public class sumloop { public static void main throws ioexception { int s=0; string output=““; bufferedreader stadin = new bufferedreader); ; string input =; for;i++) { output+=input; int a=; s+=a; } ; }}另解:import *;public class sumloop { public static void main throws ioexception { int s=0; int n; int t=0; bufferedreader stadin = new bufferedreader); string input = ; n=; for{ t=t*10+n; s=s+t; ; } ; }}

【程序9】题目:一个数如果恰好等于它的因子之和,这个数就称为 “完数 “。例如6=1+2+3.编程 找出1000以内的所有完数。public class wanshu { public static void main { int s; for { s=0; for if s=s+j;if; } ; }} 【程序10】 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?public class ex10 { public static void main { double s=0; double t=100; for { s+=t; t=t/2; } ; ; }}

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4