欢迎来到天天文库
浏览记录
ID:16704163
大小:137.00 KB
页数:40页
时间:2018-08-24
《java基础50道经典练习题及答案》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、沃克IT教育JAVA基础编程练习题整理:Lemon【程序1】题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?程序分析:兔子的规律为数列1,1,2,3,5,8,13,21....publicclassProg1{publicstaticvoidmain(String[]args){intn=10;System.out.println("第"+n+"个月兔子总数为"+fun(n));}privatestaticintf
2、un(intn){if(n==1
3、
4、n==2)return1;elsereturnfun(n-1)+fun(n-2);}}【程序2】题目:判断101-200之间有多少个素数,并输出所有素数。程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。publicclassProg2{publicstaticvoidmain(String[]args){intm=1;intn=1000;intcount=0;//统计素数个数for(inti=m;i5、if(isPrime(i)){count++;System.out.print(i+"");if(count%10==0){System.out.println();}}}otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNort6、hOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommitteeSecretary,Mi7、nistersandSystem.out.println();System.out.println("在"+m+"和"+n+"之间共有"+count+"个素数");}//判断素数privatestaticbooleanisPrime(intn){booleanflag=true;if(n==1)flag=false;else{for(inti=2;i<=Math.sqrt(n);i++){if((n%i)==08、9、n==1){flag=false;break;}elseflag=true;}}returnflag10、;}}【程序3】题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。publicclassProg3{publicstaticvoidmain(String[]args){for(inti=100;i<1000;i++){if(isLotus(i))System.out.print(i+"");}System.out11、.println();}//判断水仙花数privatestaticbooleanisLotus(intlotus){intm=0;otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganization,Secreta12、ryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecret
5、if(isPrime(i)){count++;System.out.print(i+"");if(count%10==0){System.out.println();}}}otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNort
6、hOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommitteeSecretary,Mi
7、nistersandSystem.out.println();System.out.println("在"+m+"和"+n+"之间共有"+count+"个素数");}//判断素数privatestaticbooleanisPrime(intn){booleanflag=true;if(n==1)flag=false;else{for(inti=2;i<=Math.sqrt(n);i++){if((n%i)==0
8、
9、n==1){flag=false;break;}elseflag=true;}}returnflag
10、;}}【程序3】题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。publicclassProg3{publicstaticvoidmain(String[]args){for(inti=100;i<1000;i++){if(isLotus(i))System.out.print(i+"");}System.out
11、.println();}//判断水仙花数privatestaticbooleanisLotus(intlotus){intm=0;otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganization,Secreta
12、ryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecret
此文档下载收益归作者所有