logo资料库

蓝桥杯Java历年真题及答案整理.docx

第1页 / 共101页
第2页 / 共101页
第3页 / 共101页
第4页 / 共101页
第5页 / 共101页
第6页 / 共101页
第7页 / 共101页
第8页 / 共101页
资料共101页,剩余部分请下载后查看
蓝桥杯 java 历年真题及答案整理(闭关一个月,呕心沥血整理出来的) 1. 算法是这样的,如果给定 N 个不同字符,将这 N 个字符全排列,最终的结果将会是 N!种。如:给定 A、B、C 三个不同的字符,则结果为:ABC、ACB、BAC、BCA、CAB、CBA 一共 3!=3*2=6 种情况。 package Question1_9; import java.util.Scanner; import java.util.Vector; public class Question1 { public static long count=0; private void fullPermutation(Vectorsourse, Vector result) { if(sourse.size()==0){ for (int i = 0; i < result.size(); i++) { System.out.print(result.elementAt(i)); } System.out.print("\n"); count++; return; } for (int i = 0; i < sourse.size(); i++) { Vectortsourse=new Vector(sourse); Vectortresult=new Vector(result); tresult.add(sourse.elementAt(i)); tsourse.remove(i); new Question1().fullPermutation(tsourse, tresult); } } public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); Vector sourse=new Vector(); Vector result=new Vector(); for (int i = 0; i < n; i++) { sourse.add((char)('A'+i)); }
new Question1().fullPermutation(sourse, result); System.out.println(Question1.count); } } 2. 串的简单处理 串的处理 在实际的开发工作中,对字符串的处理是最常见的编程任务。 本题目即是要求程序对用户输入的串进行处理。具体规则如下: 1. 把每个单词的首字母变为大写。 2. 把数字与字母之间用下划线字符(_)分开,使得更清晰 3. 把单词中间有多个空格的调整为1个空格。 例如: 用户输入: you and 则程序输出: You And Me What Cpp_2005_program me what cpp2005program 用户输入: this is 则程序输出: This Is A 99_cat a 99cat 我们假设:用户输入的串中只有小写字母,空格和数字,不含其它的字母或符号。 每个单词间由1个或多个空格分隔。 假设用户输入的串长度不超过200个字符。 package Question1_9; import java.util.Scanner; import java.util.Vector; public class Question2 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String string=scanner.nextLine();
Vectorvector=new Vector(); for (int i = 0; i < string.length(); i++) { vector.add(string.charAt(i)); } try { int index=0; while (index='a'&&vector.elementAt(index)<='z'){ vector.set(index, (char)(vector.elementAt(index)-('a'-'A'))); }else if(vector.elementAt(index-1)==' '&&vector.elementAt(index)==' '){ vector.remove(index); index--; }else if (vector.elementAt(index-1)==' '&&(vector.elementAt(index)>='a'&&vector.elementAt(index)<='z')) { vector.set(index, (char)(vector.elementAt(index)-('a'-'A'))); }else if((vector.elementAt(index)>='a'&&vector.elementAt(index)<='z')&&(vector.elementAt(index-1)>='0'&&vector.elementAt(in dex-1)<='9')){ vector.add(index, '_'); index++; }else if((vector.elementAt(index-1)>='a'&&vector.elementAt(index-1)<='z')&&(vector.elementAt(index)>='0'&&vector.elementAt( index)<='9')){ vector.add(index, '_'); index++; } index++; } for (int i = 0; i
} } 运行结果: you and You And Me What Cpp_2005_program me what cpp2005program 3. 猜算式 看下面的算式: □□ x □□ = □□ x □□□ 它表示:两个两位数相乘等于一个两位数乘以一个三位数。 如果没有限定条件,这样的例子很多。 但目前的限定是:这9个方块,表示1~9的9个数字,不包含0。 该算式中1至9的每个数字出现且只出现一次! 比如: 46 x 79 = 23 x 158 54 x 69 = 27 x 138 54 x 93 = 27 x 186 ..... 请编程,输出所有可能的情况! 注意: 左边的两个乘数交换算同一方案,不要重复输出! 不同方案的输出顺序不重要 package Question1_9; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.Vector; public class Question3 { if(a*b==c*d){ return true; } return false; public static long count=0; public static List> filteredNonRedundantResults; private static boolean isfilter(Vector result) { int a=(result.elementAt(0)-'0')*10+(result.elementAt(1)-'0'); int b=(result.elementAt(2)-'0')*10+(result.elementAt(3)-'0'); int c=(result.elementAt(4)-'0')*10+(result.elementAt(5)-'0'); int d=(result.elementAt(6)-'0')*100+(result.elementAt(7)-'0')*10+(result.elementAt(8)-' 0');
} public static void print(Vectorvector) { System.out.printf("%c%c %c%c x %c%c%c",vector.elementAt(0),vector.elementAt(1),vector.elementAt(2),vector.elemen tAt(3),vector.elementAt(4),vector.elementAt(5),vector.elementAt(6),vector.elementAt (7),vector.elementAt(8)); %c%c x = } result) { private static void fullPermutation(Vectorsourse, Vector if(sourse.size()==0&&isfilter(result)){ boolean exit=false; for (int i = 0; i < filteredNonRedundantResults.size(); i++) { int ra=(result.elementAt(0)-'0')*10+(result.elementAt(1)-'0'); int rb=(result.elementAt(2)-'0')*10+(result.elementAt(3)-'0'); int fa=(filteredNonRedundantResults.get(i).elementAt(0)-'0')*10+(filteredNonRedundantRe sults.get(i).elementAt(1)-'0'); fb=(filteredNonRedundantResults.get(i).elementAt(2)-'0')*10+(filteredNonRedundantRe sults.get(i).elementAt(3)-'0'); if(ra==fb&&rb==fa){ int exit=true; break; } } if(exit==false){ } return; filteredNonRedundantResults.add(new Vector(result)); } for (int i = 0; i < sourse.size(); i++) { result.add(sourse.elementAt(i)); sourse.remove(i); fullPermutation(sourse, result); sourse.add(i, result.elementAt(result.size()-1)); result.remove(result.size()-1); } } public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int n=9;
Vector sourse=new Vector(); Vector result=new Vector(); for (int i = 1; i <= n; i++) { sourse.add((char)('0'+i)); } Question3.filteredNonRedundantResults=new ArrayList>(); Question3.fullPermutation(sourse, result); for (int i = 0; i < Question3.filteredNonRedundantResults.size(); i++) { Question3.print(Question3.filteredNonRedundantResults.get(i)); System.out.println(); } } } 运行结果: 46 x 79 = 23 x 158 54 x 69 = 27 x 138 54 x 93 = 27 x 186 58 x 67 = 29 x 134 58 x 69 = 23 x 174 58 x 73 = 29 x 146 58 x 96 = 32 x 174 63 x 74 = 18 x 259 64 x 79 = 32 x 158 73 x 96 = 12 x 584 76 x 98 = 14 x 532 4. Excel 地址转换 Excel 是最常用的办公软件。每个单元格都有唯一的地址表示。 比如:第 12 行第 4 列表示为:“D12”,第 5 行第 255 列表示为“IU5”。 事实上,Excel 提供了两种地址表示方法,还有一种表示法叫做 RC 格式地址。 第 12 行第 4 列表示为:“R12C4”,第 5 行第 255 列表示为“R5C255”。 你的任务是:编写程序,实现从 RC 地址格式到常规地址格式的转换。 【输入、输出格式要求】 用户先输入一个整数 n(n<100),表示接下来有 n 行输入数据。 接着输入的 n 行数据是 RC 格式的 Excel 单元格地址表示法。 程序则输出 n 行数据,每行是转换后的常规地址表示法。 例如:用户输入:
2 R12C4 R5C255 则程序应该输出: D12 IU5 package Question1_9; import java.util.Scanner; import java.util.Stack; import java.util.Vector; public class Question4 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); scanner.nextLine(); //必须加上的,不然会导致输入不准确! while (n>0) { String string=scanner.nextLine(); String strings[]=string.split("C"); strings[0]=strings[0].substring(1, strings[0].length()); int hangshu=Integer.parseInt(strings[0]),lieshu=Integer.parseInt(strings[1]);//获取行数和列数 /* * 对列数进行变换 */ Stackstack=new Stack(); while(lieshu>0){ if(lieshu%26==0){ stack.push('Z'); lieshu=lieshu/26-1; }else { stack.push((char)('A'-1+lieshu%26)); lieshu=lieshu/26; } } while (!stack.empty()) { System.out.print(stack.pop()); } System.out.println(hangshu); n--; } } }
运行结果: 输入一个整数 n(n<100) 2 R12C4 R5C255 D12 IU5 5. 手机尾号 /* 30 年的改革开放,给中国带来了翻天覆地的变化。2011 全年中国手机产量约为 11.72 亿部。手机已经成为百姓的基 本日用品! 给手机选个好听又好记的号码可能是许多人的心愿。 但号源有限,只能辅以有偿选号的方法了。 这个程序的目的就是:根据给定的手机尾号(4 位),按照一定的规则来打分。其规则如下: 1. 如果出现连号,不管升序还是降序,都加 5 分。例如:5678,4321 都满足加分标准。 2. 前三个数字相同,或后三个数字相同,都加 3 分。例如:4888,6665,7777 都满足加分的标准。 注意:7777 因为满足这条标准两次,所以这条规则给它加了 6 分。 3. 符合 AABB 或者 ABAB 模式的加 1 分。例如:2255,3939,7777 都符合这个模式,所以都被加分。 注意:7777 因为满足这条标准两次,所以这条标准给它加了 2 分。 4. 含有:6,8,9 中任何一个数字,每出现一次加 1 分。例如 4326,6875,9918 都符合加分标准。其中,6875 被加 2 分;9918 被加 3 分。 尾号最终得分就是每条标准的加分总和! 要求程序从标准输入接收数据,在标准输出上输出结果。 输入格式为:第一行是一个整数 n(<100),表示下边有多少输入行,接下来是 n 行 4 位一组的数据,就是等待计算 加分的手机尾号。 例如,输入: 14 3045 …. ….. 6789 8866 则输出: 0 0 …. … 8 5 */ package Question1_9; import java.util.Scanner;
分享到:
收藏