《java面试题基本算法》由会员分享,可在线阅读,更多相关《java面试题基本算法(4页珍藏版)》请在金锄头文库上搜索。
1、1、给出十个整数(int型),输入任意一个数字查询是否存在该10个数字内。publicclassDemo31publicstaticvoidmain(Stringargs)inttemp=1,2,3,4,5,6,7,8,9,10;booleanflag=false;intx=15;for(inti=0;itemp.length;i+)if(x=temp)flag=true;/如果已经找到了,则直接退出循环即可break;if(flag)System.out.println(”已经发现了内容。);elseSystem.out.println(”没有发现内容。);2、定义一个包含10个元素的数组,
2、对其进行赋值使其每个元素的值等于其下标,然后输出;接着将这个数组倒置(即,首尾交换)后输出;publicclassDemo32publicstaticvoidmain(Stringargs)inti=newint10;for(intx=0;xi.length;x+)ix=x;/进行倒置intj=newint;inty=j.length-1;for(intx=0;xi.length;x+,y-)jy=ix;for(intx=0;xi.length;x+)System.out.print(jx+t);3、给出十个老师的打分,对十个老师的打分找到最高分。publicclassDemo33public
3、staticvoidmain(Stringargs)inti=23,54,12,45,78,90,100;intmax=i0;for(intx=0;xmax)max=ix;System.out.println(最高分:+max);4、输入30个09之间的数字,分别统计09这10个数字分别出现了多少次。publicclassDemo34publicstaticvoidmain(Stringargs)inti=0,0,9,8,7,6,6,7,8,8,5,4,3,4,5,6,6,3,2,2,1;/定义一个数组,用来统计09的数字个数intcount=newint10;for(intx=0;xi.le
4、ngth;x+)switch(ix)case0:count0+;break;case1:count1+;break;case2:count2+;break;case3:count3+;break;case4:count4+;break;case5:count5+;break;case6:count6+;break;case7:count7+;break;case8:count8+;break;case9:count9+;break;inttemp=0;for(intx=0;xcount.length;x+)System.out.println(temp+的出现次数:+countx);temp+
5、;5、定义一个整型数组,保存10个数据,利用程序完成将最大值保存在数组中第一个元素。?直接把第一个数字和最大的数字的位置交换。这样一来则前后的顺序就要改变?0123456789876publicclassDemo35publicstaticvoidmain(Stringargs)inti=1,2,3,54,5,67,8,0;/求出最大值的位置intmax=i0;/表示现在最大值的下标intmax_foot=0;for(intx=0;xi.length;x+)if(maxix)max=ix;max_foot=x;inttemp=i0;i0=imax_foot;imax_foot=temp;for
6、(intx=0;xi.length;x+)System.out.print(ix+t);6、对排序好的数组,添加一个数字,将添加后的数字插入到数组合适的位置。留一个位置?如果数组要想添加数字,则必须有一个位置让用户添加,所以开辟一个数组,出来用于添加数据。publicclassDemo36publicstaticvoidmain(Stringargs)inti=newint10;i0=2;i1=1;i2=43;i3=23;i4=2;i5=56;i6=7;i7=8;i8=9;java.util.Arrays.sort(i);i9=10;java.util.Arrays.sort(i);for(intx=0;xi.length;x+)System.out.print(ix+t);