JAVA编程题全集(50题及答案).docx

上传人:太** 文档编号:72807175 上传时间:2023-02-13 格式:DOCX 页数:24 大小:46.03KB
返回 下载 相关 举报
JAVA编程题全集(50题及答案).docx_第1页
第1页 / 共24页
JAVA编程题全集(50题及答案).docx_第2页
第2页 / 共24页
点击查看更多>>
资源描述

《JAVA编程题全集(50题及答案).docx》由会员分享,可在线阅读,更多相关《JAVA编程题全集(50题及答案).docx(24页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、【程序1】题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?这是一个菲波拉契数列问题public class lianxiOl public static void main(String args) (第 1 个月的兔子对数:I);(第 2 个月的兔子对数:1);int fl = l,f2= 1, f, M=24;for(int i=3; i=M: i+) f=f2;f2 = fl + f2;fl =f;System-out.printlnCM + i +”个月的兔子对数:+f2);【程序2】题

2、目:判断101-200之间有多少个素数,并输出所有素数。程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明 此数不是素数,反之是素数。public class lianxi02 public static void main(String args) int count = 0;for(int i=101;i200; i+=2) boolean b = false;for(int j=2; j=Math.sqrt(i): j+)if(i%j= o) b = false; break; else b = true; Iif(b = true) count +;

3、System.out.println(i);)(素数个数足:+ count);)【程序3】题目:打印出所有的“水仙花数,所谓“水仙花数”是指一个三位数,其各位数字立方和 等于该数本身。例如:153是一个”水仙花数,因为153=1的三次方+5的三次方+ 3的 三次方。public class lianxi03 public static void main(String args) int bl, b2, b3;【程序17题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一 个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩 下的一半零一个。

4、到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。public class Iianxil7 public static void main(String args) int x = 1;for(int i=2; i=10; i+) x = (x+l)*2;)(猴子第一天摘 了 + x + 个桃子);)【程序18题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决 定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序 找出三队赛手的名单。public class lianxi!8 static char m =

5、a, b, c ;staticcharn = x; y; z ;public static void main(String args) for (int i = 0; i m.length; i+) for (int j = 0; j n.length: j+) if (mi = a & njj - x) continue; else if (mi = a & nj = y) continue; else if (mi = c & nj = x)II (mi = *c & nU = *z) continue;| else if(mi = V & n(j = z) II (mil = b &

6、nj = y) continue; elseSystem.out.println(mi + vs + nj);)【程序19)题FI:打印出如下图案(菱形) *public class Iianxil9 public static void main(String args) intH = 7,W = 7力高和宽必须是相等的奇数for(int i=0; i(H+1)/2; i+) Mint j=0; jW/2-i;j+) Systcm.out.print();)for(int k= 1; k(i+1)*2: k+) System.out.print(*);)Systern.out.println

7、();)for(int i=l; i=H/2; i+) for(intj=l;j=i;j+) System.out.print();Ifor(int k=l; k=W-2*i; k+) Systeni.out.print(*);ISystcm.out.println();【程序20题目:有一分数序列:2/1, 3/2, 5/3, 8/5, 13/8, 21/13求出这个数列的前20项之和。public class lianxi20 public static void main(String args) int x = 2, y = 1, t;double sum = 0;for(int i=

8、l;i=20; i+) sum = sum + (double)x / y;t = y;y = x;x = y + t;ISystem.out.println(nHU 20 项相加之和是:+ sum);I【程序21题目:求1+2!求!+.+20!的和public class lianxi21 public static void main(String args) long sum = 0;long fac = 1;for(int i=l; i=20; i+) fac = fac * i;sum += fac;System.out.println(sum);【程序22题目:利用递归方法求5!。

9、public class Iianxi22 public static void main(String args) int n = 5;rcc fr = new rcc();Systein.ou(.println(n+! = +fr.rec(n);)class recpublic long rec(int n)(long value = 0 ;if(n =1 ) value = 1; else value = n * rec(n-l);return value;II【程序23题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数, 他说比第3个人大2岁。问第三个人,又

10、说比第2人大两岁。问第2个人,说比第一个人大 两岁。最后问第一个人,他说是10岁。请问第五个人多大?public class lianxi23 public static void main(String args) int age = 10;for(int i=2; i=0; i) System, out. print(chi);)【程序25题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与方位相同,十位与千 位相同。import java. util. *:public class lianxi25 public static void main(String args)

11、Scanner s = new Scanner(System, in); int a;doSystem. out. print(“请输入一个5位正整数:);a = s. nextlnt ();)while(a99999);String ss =String. valueOf(a); char ch = ss. toCharArray(); if (ch 0 =ch 4 &ch 1 =ch 3) System. out. print In (这是一个回文数”); else System, out. printing这不是一个回文数); 这个更好,不限位数 import java, util.*;

12、 public class lianxi25a public static void main(String args) Scanner s = new Scanner(System, in); boolean is =true;System. out. print (请输入一个正整数:); long a = s. nextLongO ;String ss = Long. toString(a);chart ch = ss. toCharArray();int j=ch. length;for(int i=0; ij/2; i+) if (chi!=chj-i-l)is=false;)if(i

13、s=true) System, out. printin(这是一个回文数); else (System, out. printin(这不是一个回文数”); )【程序26题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继 续判断第二个字母。import java. util. *:public class lianxi26 public static void main(String args) getChar tw = new getChar();System, out. println(请输入星期的第一个大写字母:); char ch = tw. getChar ()

14、;switch (ch) case M:System. out. printin(Monday);break;case W:System, out. print In(Wednesday);break;case F:System, out. printin(Friday);break;case T : System. out. print In (请输入星期的第二个字母:”);char ch2 = tw. getChar();if (ch2 = U) System, out. print In (wTuesday,/); else i f(ch2 = H) System, oul. print

15、 In(Thursday); else System. out. printin(无此写法!“);) ); break;case S : System, out. printin(请输入星期的第二个字母:”);char ch2 = tw. getChar();if(ch2 = U) System. out. print In(Sunday); else if(ch2 = A) System, out. printIn(Saturday); else System. out. print In (无此写法!“););break;default: System, out. print In (无此

16、写法!”);)class getCharpublic char getChar() Scanner s = new Scanner(System, in);String str = s. nextLineO ;char ch = str. charAt (0);if(chZ) System. out. printin(输入错误,请重新输入”);ch二getChar();)return ch;)【程序27题目:求100之内的素数使用除sqrt (n)的方法求出的素数不包括2和3public class lianxi27 public static void main(String args) b

17、oolean b =false;System, out. print(2 + ;System, out. print(3 + ;for(int i=3; i100; i+=2) for(int j=2; j=Math. sqrt(i); j+) if(i % j = 0) (b = false;break; elseb = true;)if(b = true) System. out. print(i + );)该程序使用除1位素数得2位方法,运行效率高通用性差。public class lianxi27a (public static void main(String args) int a

18、 = new int 2, 3, 5, 7;for(int j=0; j4; j+)System, out. print(aj + ; boolean b =false;for(int i=ll; i100; i+=2) for(int j=0; j4; j+) if(i % aj = 0) b = false;break; elsefb = true;)if(b = true) System. out. print(i + );)【程序28题目:对10个数进行排序import java, util.*;public class lianxi28 public static void main

19、(String args) Scanner s = new Secinner (System, in);int a = new int10;System. out. printin(”请输入 10 个整数:“);for(int i=0; i10; i+) ai = s. nextlnt();)for(int i=0; i10; i+) for(int j=i+l; j aj) int t = ai; ai = aj; aj = t;)for(int i=0; i10; i+) System. out. print(ai + “ );)【程序29题目:求一个3*3矩阵对角线元素之和import

20、java, util.*;public class lianxi29 public static void main(String args) Scanner s = new Scanner(System, in); int a = new int33;System. out. printin(“请输入 9 个整数:);for(int i=0; i3; i+) for(int j=0; j3; j+) ai j = s. nextlnt ();)System. out. print In (输入的 3 * 3 矩阵是:);for(int i=0; i3; i+)(for(int j=0; j3

21、; j+) System. out. print(aij + “;)System. out. printlnO ;)int sum = 0;for(int i=0; i3; i+) for(int j=0; j= aa. length-1) bb. length-1 = num;for(i=0; ia. length; i+) bi = ai;) else for(i=0; i= ai) bi = ai; else ( bi = num; break;)for(int j=i+l; jb.length; j+) bj = aj-l;)for (i = 0; i b. length; i+) S

22、ystem. out. print(bi + ;)【程序31题目:将一个数组逆序输出。import java, util.*;public class lianxi31 public static void main(String args) Scanner s = new Sc=0; j=j-l) System. out. print (aj+z,);)【程序32题目:取一个整数a从右端开始的47位。import java. util. *;public class lianxi32 public static void main(String args) Secinner s = new

23、Scanner (System, in);System. out. print (请输入一个7位以上的正整数:“);long a = s. nextLongO ;String ss = Long. toString(a);char ch = ss.toCharArray();int j=ch. length;if (j7) System. out. printin(输入错误!”); else System, out. printlnC,截取从右端开始的47 位是: +chj-7+chj-6+chj-5+ch j-4);))【程序33题目:打印出杨辉三角形(要求打印出10行如下图)1111314

24、510121316411051 public class lianxi33 public static void main(String args) int a = new int1010;for(int i=0; i10; i+) aii = 1;ai0 = 1;for(int i=2; i10; i+) for(int j=l; ji; j+) aij = ai-lj-l + ai-l j;)for(int i=0; i10; i+) for(int k=0; k2*(10-i)-l; k+) System, out. print”);)for(int j=0; j=i; j+) Syst

25、em, out. print(aij +);for(int m=101; m1000; m+) b3 = m / 100;h2 = m % 100/ 10;bl =m% 10;if(b3*b3*b3 + b2*b2*b2 + b 1 *b 1 *b 1) = m) System.out.prinUn(m+”是一个水仙花数);【程序4】题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。程序分析:对n进行分解质因数,应先找到一个最小的质数匕然后按下述步骤完成:(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。(2)如果nk,但n能被k整除,则应打印出k

26、的值,并用n除以k的商,作为新的正整数 你n,重复执行第一步。如果n不能被k整除,则用k+1作为k的值,重复执行第一步。import java.util.*;public class lianxi04public static void main(String args) Scanner s = new Scanner(System.in);Systcm.out.print(请键入一个正整数:);int n = s.nextlnt();int k=2;Systcm.out.print(n +);while(k =90分的同学用A表示,60-89分之 间的用B表示,60分以下的用C表示。impo

27、rt java.util.*;public class lianxi05 public static void main(String args) int x;char grade;Scanner s = new Scanncr(System.in);(请输入一个成绩:);x = s.nextlnt();grade = x = 90 ? A:x = 60 ? BSystem, out. printlnO ;)【程序34题目:输入3个数a,b,c,按大小顺序输出。import java. util. Scanner;public class lianxi34 public static void

28、 mciin(String args) Scanner s = new Scanner(System, in);System. out. printin(请输入 3 个整数:);int a = s. nextlnt ();int b = s. nextlnt ();int c = s. nextlnt (); if(a b) int t = a;a = b;b = t;)if (a c) int t = a;a = c; c = t;)if(b c)( int t = b; b = c;c = t;)System. out. printin(从大到小的顺序输出:);System, out. p

29、rintin(a + “ + b + + c);)【程序35题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。 import java, util.*;public class lianxi35 public static void main(String args) int N = 8;int a = new int N;Scanner s = new Scanner(System, in);int idxl = 0, idx2 = 0;System. out. print In(”请输入 8 个整数:);for(int i=0; iN; i+) ai = s. n

30、extlnt ();System. out. printin(你输入的数组为:”); for(int i=0; iN; i+) System, out. print(ai + ;)int max =a0, min = a0;for(int i=0; i max) max = ai;idxl = i;)if(ai min) min = ai;idx2 = i;)if(idxl != 0) int temp = a0;a0 = aidxl;aidxl = temp;)if(idx2 != N-l) int temp = aN-l; aN-l = aidx2; aidx2 = temp;)Syste

31、m. out. print In (n 交换后的数组为:”);for(int i=0; iN; i+) System, out. print(ai + ;)【程序36题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数 import java. util. Scanner;public class lianxi36 public static void main(String args) int N =10;int a = new intN;Scanner s = new Scanner(System, in);System, out. print In (”请输入

32、10 个整数:”);for(int i=0; iN; i+) ai = s. nextlnt ();)System. out. print (你输入的数组为:”);for(int i=0; iN; i+) System, out. print(ai + ;)System. out. printCn请输入向后移动的位数:”);int m = s. nextlnt ();int b = new intm;for(int i=0; i=m; i) ai = ai-m;)for(int i=0; im; i+) ai = bi;)System. out. print (位移后的数组是:“);for(i

33、nt i=0; iN; i+)(System. out. print(ai + );)【程序371题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3 的人退出圈子,问最后留下的是原来第几号的那位。import java. util. Scanner;public class lianxi37 public static void main(String args) Scanner s = new Scanner(System, in);System, out. print (请输入排成一圈的人数:”);int n = s. nextlnt ();boolean ar

34、r = new booleann;for(int i=0; i 1) if(arrindex = true) countNum +;if(countNum = 3) countNum =0;arrindex = false; leftCount 一;)index +;if(index = n) index = 0;)for(int i=0; in; i+)(if(arri = true) System, out. printin(原排在第+(i+l)+位的人留下了。”);)【程序38题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。*题目意思似乎不能用length

35、。函数*/import java. util. *;public class 1ianxi38 public static void main(String args) Scanner s = new Scanner (Sjrstcm. in);System. out. printin (请输入一个字符串:);String str = s. ncxtLineO ;System. out. printin(字符串的长度是:+str. length。);)【程序39题目:编写一个函数,输入n为偶数时,调用函数求l/2+1/4+.+l/n,当输入n为奇数时, 调用函数1/1+1/3+. +l/n(利

36、用指针函数)没有利用指针函数import java. util. *;public class lianxi39 public static void main(String args) Scanner s = new Scanner(System, in);System. out. print(请输入一个正整数 n=);int n = s. nextlnt ();System, out. printin(相应数列的和为:“+ sum(n);)public static double sum(int n) double res = 0;if(n % 2 = 0) for(int i=2; i=

37、n; i+=2) res += (double)1 / i;) else for(int i=l; i=n; i+=2) res += (double)1 / i ;)return res;【程序40题目:字符串排序。public class lianxi40 public static void main(String args) int N=5;String temp = null;String s = new StringN;s0 = matter”;sl = state;s2 = solid;s3 = liquid;s4 = gas;for(int i=0; iN; i+)(for(i

38、nt j=i+l; jN; j+) if(compare(si sj) = false) temp = si;si = sj;sj = temp;)for(int i=0; iN; i+)(System. out. println(si);)static boolean compare(String si, String s2) boolean result = true;for(int i=0; isl. lengthO & i s2. charAt(i) result = false;break; else if(si. charAt(i) s2. charAt (i) result = true;break; else if (si. length() s2. length () result = true; else result = false;)return resu

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 解决方案

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com