java历年真题及答案整理.docx
- 文档编号:2866807
- 上传时间:2022-11-16
- 格式:DOCX
- 页数:135
- 大小:86.04KB
java历年真题及答案整理.docx
《java历年真题及答案整理.docx》由会员分享,可在线阅读,更多相关《java历年真题及答案整理.docx(135页珍藏版)》请在冰豆网上搜索。
java历年真题及答案整理
蓝桥杯2014年以前java历年真题及答案整理
1、字符排序
如果给定N个不同字符,将这N个字符全排列,最终的结果将会是N!
种。
如:
给定A、B、C三个不同的字符,则结果为:
ABC、ACB、BAC、BCA、CAB、CBA一共3!
=3*2=6种情况。
publicclassQuestion1{
publicstaticlongcount=0;
privatevoidfullPermutation(Vector
if(sourse.size()==0){
for(inti=0;i System.out.print(result.elementAt(i)); } System.out.print("\n"); count++; return; } for(inti=0;i Vector Vector tresult.add(sourse.elementAt(i)); tsourse.remove(i); newQuestion1().fullPermutation(tsourse,tresult); } } publicstaticvoidmain(String[]args){ Scannerscanner=newScanner(System.in); intn=scanner.nextInt(); Vector Vector for(inti=0;i sourse.add((char)('A'+i)); } newQuestion1().fullPermutation(sourse,result); System.out.println(Question1.count); }} 2、串的简单处理 串的处理 在实际的开发工作中,对字符串的处理是最常见的编程任务。 本题目即是要求程序对用户输入的串进行处理。 具体规则如下: 1.把每个单词的首字母变为大写。 2.把数字与字母之间用下划线字符(_)分开,使得更清晰 3.把单词中间有多个空格的调整为1个空格。 例如: 用户输入: youandmewhatcpp2005program 则程序输出: YouAndMeWhatCpp_2005_program 用户输入: thisisa99cat 则程序输出: ThisIsA99_cat 我们假设: 用户输入的串中只有小写字母,空格和数字,不含其它的字母或符号。 每个单词间由1个或多个空格分隔。 假设用户输入的串长度不超过200个字符。 publicclassQuestion2{ publicstaticvoidmain(String[]args){ Scannerscanner=newScanner(System.in); Stringstring=scanner.nextLine(); Vector for(inti=0;i vector.add(string.charAt(i)); } try{ intindex=0; while(index if(index==0&&vector.elementAt(index)>='a'&&vector.elementAt(index)<='z'){ vector.set(index,(char)(vector.elementAt(index)-('a'-'A'))); }elseif(vector.elementAt(index-1)==''&&vector.elementAt(index)==''){ vector.remove(index); index--; }elseif(vector.elementAt(index-1)==''&&(vector.elementAt(index)>='a'&&vector.elementAt(index)<='z')){ vector.set(index,(char)(vector.elementAt(index)-('a'-'A'))); }elseif((vector.elementAt(index)>='a'&&vector.elementAt(index)<='z')&&(vector.elementAt(index-1)>='0'&&vector.elementAt(index-1)<='9')){ vector.add(index,'_'); index++; }elseif((vector.elementAt(index-1)>='a'&&vector.elementAt(index-1)<='z')&&(vector.elementAt(index)>='0'&&vector.elementAt(index)<='9')){ vector.add(index,'_'); index++; } index++; } for(inti=0;i System.out.print(vector.elementAt(i)); } System.out.println(); }catch(ArrayIndexOutOfBoundsExceptione){ //TODO: handleexception }}} 运行结果: youandmewhatcpp2005program YouAndMeWhatCpp_2005_program 3、猜算式 看下面的算式: □□x□□=□□x□□□ 它表示: 两个两位数相乘等于一个两位数乘以一个三位数。 如果没有限定条件,这样的例子很多。 但目前的限定是: 这9个方块,表示1~9的9个数字,不包含0。 该算式中1至9的每个数字出现且只出现一次! 比如: 46x79=23x158 54x69=27x138 54x93=27x186 ..... 请编程,输出所有可能的情况! 注意: 左边的两个乘数交换算同一方案,不要重复输出! 不同方案的输出顺序不重要 publicclassQuestion3{ publicstaticlongcount=0; publicstaticList privatestaticbooleanisfilter(Vector inta=(result.elementAt(0)-'0')*10+(result.elementAt (1)-'0'); intb=(result.elementAt (2)-'0')*10+(result.elementAt(3)-'0'); intc=(result.elementAt(4)-'0')*10+(result.elementAt(5)-'0'); intd=(result.elementAt(6)-'0')*100+(result.elementAt(7)-'0')*10+(result.elementAt(8)-'0'); if(a*b==c*d){ returntrue; } returnfalse; } publicstaticvoidprint(Vector System.out.printf("%c%cx%c%c=%c%cx%c%c%c",vector.elementAt(0),vector.elementAt (1),vector.elementAt (2),vector.elementAt(3),vector.elementAt(4),vector.elementAt(5),vector.elementAt(6),vector.elementAt(7),vector.elementAt(8)); } privatestaticvoidfullPermutation(Vector if(sourse.size()==0&&isfilter(result)){ booleanexit=false; for(inti=0;i intra=(result.elementAt(0)-'0')*10+(result.elementAt (1)-'0'); intrb=(result.elementAt (2)-'0')*10+(result.elementAt(3)-'0'); intfa=(filteredNonRedundantResults.get(i).elementAt(0)-'0')*10+(filteredNonRedundantResults.get(i).elementAt (1)-'0'); intfb=(filteredNonRedundantResults.get(i).elementAt (2)-'0')*10+(filteredNonRedundantResults.get(i).elementAt(3)-'0'); if(ra==fb&&rb==fa){ exit=true; break; } } if(exit==false){ filteredNonRedundantResults.add(newVector } return; } for(inti=0;i result.add(sourse.elementAt(i)); sourse.remove(i); fullPermutation(sourse,r
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 历年 答案 整理