现在的位置: 首页 > 编程语言 > 正文

java蓝桥杯历年真题及答案整理(小结)

2020年02月13日 编程语言 ⁄ 共 30793字 ⁄ 字号 评论关闭

蓝桥杯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(Vector<Character>sourse, Vector<Character> 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++) { Vector<Character>tsourse=new Vector<Character>(sourse); Vector<Character>tresult=new Vector<Character>(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<Character> sourse=new Vector<Character>(); Vector<Character> result=new Vector<Character>(); for (int i = 0; i < n; i++) { sourse.add((char)('A'+i)); } new Question1().fullPermutation(sourse, result); System.out.println(Question1.count); }}

2串的简单处理

串的处理

在实际的开发工作中,对字符串的处理是最常见的编程任务。本题目即是要求程序对用户输入的串进行处理。具体规则如下:

    把每个单词的首字母变为大写。 把数字与字母之间用下划线字符(_)分开,使得更清晰 把单词中间有多个空格的调整为1个空格。

例如:用户输入:you and me what cpp2005program则程序输出:You And Me What Cpp_2005_program用户输入:this is a 99cat则程序输出:This Is A 99_cat

我们假设:用户输入的串中只有小写字母,空格和数字,不含其它的字母或符号。每个单词间由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(); Vector<Character>vector=new Vector<Character>(); for (int i = 0; i < string.length(); i++) { vector.add(string.charAt(i)); } try { int index=0; while (index<vector.size()) { if(index==0&&vector.elementAt(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(index-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 <vector.size(); i++) { System.out.print(vector.elementAt(i)); } System.out.println(); } catch (ArrayIndexOutOfBoundsException e) { // TODO: handle exception } }}

运行结果:

you and me what cpp2005programYou And Me What Cpp_2005_program

3猜算式

看下面的算式:□□ x □□ = □□ x □□□

它表示:两个两位数相乘等于一个两位数乘以一个三位数。如果没有限定条件,这样的例子很多。但目前的限定是:这9个方块,表示1~9的9个数字,不包含0。该算式中1至9的每个数字出现且只出现一次!比如:46 x 79 = 23 x 15854 x 69 = 27 x 13854 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 { public static long count=0; public static List<Vector<Character>> filteredNonRedundantResults; private static boolean isfilter(Vector<Character> 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'); if(a*b==c*d){ return true; } return false; } public static void print(Vector<Character>vector) { System.out.printf("%c%c x %c%c = %c%c x %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)); } private static void fullPermutation(Vector<Character>sourse, Vector<Character> result) { 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+(filteredNonRedundantResults.get(i).elementAt(1)-'0'); int fb=(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(new Vector<Character>(result)); } return; } 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<Character> sourse=new Vector<Character>(); Vector<Character> result=new Vector<Character>(); for (int i = 1; i <= n; i++) { sourse.add((char)('0'+i)); } Question3.filteredNonRedundantResults=new ArrayList<Vector<Character>>(); 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 15854 x 69 = 27 x 13854 x 93 = 27 x 18658 x 67 = 29 x 13458 x 69 = 23 x 17458 x 73 = 29 x 14658 x 96 = 32 x 17463 x 74 = 18 x 25964 x 79 = 32 x 15873 x 96 = 12 x 58476 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行数据,每行是转换后的常规地址表示法。例如:用户输入:2R12C4R5C255则程序应该输出:D12IU5

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]);//获取行数和列数 /* * 对列数进行变换 */ Stack<Character>stack=new Stack<Character>(); 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)2R12C4R5C255D12IU5

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位一组的数据,就是等待计算加分的手机尾号。例如,输入:143045….…..67898866则输出:00….…85*/package Question1_9;import java.util.Scanner;import java.util.Stack;import java.util.Vector;public class Question5 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); scanner.nextLine(); while ((n--)>0) { String telphone=scanner.nextLine(); int sum=0; /* * 情况一 */ if(telphone.charAt(0)-telphone.charAt(1)==1){ char ch=telphone.charAt(0); int index=0; while (index<4&&ch==telphone.charAt(index)) { ch--; index++; } if(index>=4){ sum+=5; } } if (telphone.charAt(0)-telphone.charAt(1)==-1) { char ch=telphone.charAt(0); int index=0; while (index<4&&ch==telphone.charAt(index)) { ch++; index++; } if(index>=4){ sum+=5; } } /* * 情况二 */ if (telphone.charAt(0)==telphone.charAt(1)&&telphone.charAt(1)==telphone.charAt(2)) { sum+=3; } if(telphone.charAt(1)==telphone.charAt(2)&&telphone.charAt(2)==telphone.charAt(3)){ sum+=3; } /* * 情况三 */ if(telphone.charAt(0)==telphone.charAt(1)&&telphone.charAt(2)==telphone.charAt(3)){ sum+=1; } if(telphone.charAt(0)==telphone.charAt(2)&&telphone.charAt(1)==telphone.charAt(3)){ sum+=1; } /* * 情况四 */ for (int i = 0; i < 4; i++) { if(telphone.charAt(i)=='6'||telphone.charAt(i)=='8'||telphone.charAt(i)=='9'){ sum+=1; } } System.out.println(sum); } }}

运行结果:1430450211…..…..……85

6. 括号问题

下面的代码用于判断一个串中的括号是否匹配 所谓匹配是指不同类型的括号必须左右呼应,可以相互包含,但不能交叉例如:..(..[..]..).. 是允许的..(...[...)....].... 是禁止的对于 main 方法中的测试用例,应该输出:falsetruefalsefalse请分析代码逻辑,并推测划线处的代码。答案写在 “解答.txt” 文件中注意:只写划线处应该填的内容,划线前后的内容不要抄写。

import java.util.*;public class Demo06 { public static boolean isGoodBracket(String s) { Stack<Character> a = new Stack<Character>(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '(') a.push(')'); if (c == '[') a.push(']'); if (c == '{') a.push('}'); if (c == ')' || c == ']' || c == '}') { if (a.size()==0) return false; // 填空 if (a.pop() != c) return false; } } if (a.size()!=0) return false; // 填空 return true; } public static void main(String[] args) { System.out.println(isGoodBracket("...(..[.)..].{.(..).}...")); System.out.println(isGoodBracket("...(..[...].(.).).{.(..).}...")); System.out.println(isGoodBracket(".....[...].(.).){.(..).}...")); System.out.println(isGoodBracket("...(..[...].(.).){.(..)....")); }}

运行结果:falsetruefalsefalse

7. 扑克牌移动

/*下面代码模拟了一套扑克牌(初始排序A~K,共13张)的操作过程。操作过程是:手里拿着这套扑克牌,从前面拿一张放在后面,再从前面拿一张放桌子上,再从前面拿一张放在后面,....如此循环操作,直到剩下最后一张牌也放在桌子上。下面代码的目的就是为了求出最后桌上的牌的顺序。初始的排列如果是A,2,3...K,则最后桌上的顺序为:[2, 4, 6, 8, 10, Q, A, 5, 9, K, 7, 3, J]请分析代码逻辑,并推测划线处的代码。答案写在 “解答.txt” 文件中注意:只写划线处应该填的内容,划线前后的内容不要抄写。*/package Question1_9;import java.util.Arrays;import java.util.List;import java.util.Vector;public class Question7 { public static List moveCard(List src) { if (src == null) return null; List dst = new Vector(); for (;;) { if (src.size()==0) break; // 填空 src.add(src.remove(0)); dst.add(src.remove(0)); // 填空 } return dst; } public static void main(String[] args) { List a = new Vector(); a.addAll(Arrays.asList("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K")); System.out.println(moveCard(a)); }}

运行结果:[2, 4, 6, 8, 10, Q, A, 5, 9, K, 7, 3, J]

8. 第一个数字

/*以下的静态方法实现了:把串s中第一个出现的数字的值返回。如果找不到数字,返回-1例如:s = "abc24us43" 则返回2s = "82445adb5" 则返回8s = "ab" 则返回-1请分析代码逻辑,并推测划线处的代码。答案写在 “解答.txt” 文件中注意:只写划线处应该填的内容,划线前后的内容不要抄写。*/public class Demo04 { public static int getFirstNum(String s) { if (s == null || s.length() == 0) return -1; char c = s.charAt(0); if (c >= '0' && c <= '9') return s.charAt(0)-'0'; // 填空 return getFirstNum(s.substring(1)); // 填空 } public static void main(String[] args) { String s1 = "abc24us43"; //则返回2 String s2 = "82445adb5"; //则返回8 String s3 = "ab"; //则返回-1 System.out.println(getFirstNum(s1)); System.out.println(getFirstNum(s2)); System.out.println(getFirstNum(s3)); }}

运行结果:28-1

9. 放麦子

/* 你一定听说过这个故事。国王对发明国际象棋的大臣很佩服,问他要什么报酬,大臣说:请在第1个棋盘格放1粒麦子,在第2个棋盘格放2粒麦子,在第3个棋盘格放4粒麦子,在第4个棋盘格放8粒麦子,......后一格的数字是前一格的两倍,直到放完所有棋盘格(国际象棋共有64格)。国王以为他只是想要一袋麦子而已,哈哈大笑。当时的条件下无法准确计算,但估算结果令人吃惊:即使全世界都铺满麦子也不够用!请你借助计算机准确地计算,到底需要多少粒麦子。答案写在“解答.txt”中,不要写在这里!*/package Question1_9;import java.math.BigInteger;public class Question9 { public static void main(String[] args) { BigInteger total=new BigInteger("0"); BigInteger base=new BigInteger("2"); for (int i = 0; i < 64; i++) { total=total.add(base.pow(i)); //System.out.println(total); } System.out.println(total); System.out.println(base.pow(64).add(new BigInteger("-1"))); }}

运行结果:18446744073709551614

10. 求21位数的水仙花数

package Question10_19;import java.math.BigInteger;import java.util.Scanner;class Question10Think2OptimizeMustRemember { public static int size; public static int array[]={0,1,2,3,4,5,6,7,8,9}; public static BigInteger powArray[] = new BigInteger[10]; // 记录0~9的size次方 public static int usedTimes[]=new int[10];// 记录0~9的使用次数 public static BigInteger iPowSizeMultiplyj[][]; //记录0到9中任意数字i的N次方乘以i出现的次数j的结果(i^N*j) public static BigInteger MAX; // size位的数字能表示的最大值 public static BigInteger MIN; // size位的数字能表示的最小值 public static void init() {// 用于初始化powArray[],MAX,MIN for (int i = 0; i < 10; i++) {// 初始化powArray[] powArray[i] = (new BigInteger("" + i)).pow(size); } MIN = (new BigInteger("10")).pow(size - 1); // 初始化最小值 MAX = (new BigInteger("10").pow(size).add(new BigInteger("-1")));// 初始化最大值 iPowSizeMultiplyj=new BigInteger[10][size+1]; //初始化iPowSizeMultiplyj[][] for (int i = 0; i < 10; i++) { iPowSizeMultiplyj[i][0]=BigInteger.valueOf(0); for (int j = 1; j < size+1; j++) { iPowSizeMultiplyj[i][j]=iPowSizeMultiplyj[i][j-1].add(powArray[i]); } } } public static void exhaustion(int arrayIndex,int used,BigInteger current) { if (current.compareTo(MAX)>1) {//超过最大值,递归结束 return; } if(used==size){//size位全部分配完毕 if(current.compareTo(MIN)<0){ //已获得的值小于最小值 return; }else { String s=current+""; int avaliableValueUsed[]=new int[10]; for (int i = 0; i < s.length(); i++) { avaliableValueUsed[s.charAt(i)-'0']++; } for (int i = 0; i < 10; i++) { if(usedTimes[i]!=avaliableValueUsed[i]){ return; } } System.out.println(current); return; } } if(arrayIndex==0){ usedTimes[0]=size-used; exhaustion(-1, size, current); usedTimes[0]=0; return; } if(current.add(iPowSizeMultiplyj[arrayIndex][size-used]).compareTo(MIN)<0){ return; } if(arrayIndex>=0){ for (int i = 0; i <= size-used; i++) { if(current.add(iPowSizeMultiplyj[arrayIndex][i]).compareTo(MAX)>0){ return; } usedTimes[arrayIndex]=i; exhaustion(arrayIndex-1, used+i,current.add(iPowSizeMultiplyj[arrayIndex][i])); usedTimes[arrayIndex]=0; } }else { return;//1到9已分配完毕,不可再延伸了 } } public static void main(String[] args) {// Scanner scanner = new Scanner(System.in);// Question10Think2.size = scanner.nextInt(); long startTime = System.currentTimeMillis(); // 程序开始时间 Question10Think2OptimizeMustRemember.size=21; Question10Think2OptimizeMustRemember.init(); Question10Think2OptimizeMustRemember.exhaustion(9, 0, BigInteger.valueOf(0)); long endTime = System.currentTimeMillis(); // 程序结束时间 System.out.println((endTime-startTime)/1000f+"秒"); // 运行总时 }}

运行结果:128468643043731391252449177399146038697307

19.062秒

11. 猜生日

/* 今年的植树节(2012年3月12日),小明和他的叔叔还有小伙伴们一起去植树。休息的时候,小明的同学问他叔叔多大年纪,他叔叔说:“我说个题目,看你们谁先猜出来!”“把我出生的年月日连起来拼成一个8位数(月、日不足两位前补0)正好可以被今天的年、月、日整除!”他想了想,又补充到:“再给个提示,我是6月出生的。”根据这些信息,请你帮小明算一下,他叔叔的出生年月日。答案写在“解答.txt”中,不要写在这里!格式是年月日连成的8位数。例如,如果是1948年6月12日,就写:19480612 */package Question10_19;public class Question11 { public static void main(String[] args) { for (int i = 20120312; ; i--) { String s=""+i; int year=Integer.parseInt(s.substring(0, 4)); int month=Integer.parseInt(s.substring(4, 6)); int day=Integer.parseInt(s.substring(6, 8)); // System.out.println(year+" "+month+" "+day); if(day==0||day>31){ continue; } if(!(i%2012==0&&i%3==0&&i%12==0)){ continue; } if(month!=6){ continue; } System.out.println(i); break; } }}

运行结果:19550604

12. 填算式

/*看这个算式:☆☆☆ + ☆☆☆ = ☆☆☆如果每个五角星代表 1 ~ 9 的不同的数字。这个算式有多少种可能的正确填写方法?173 + 286 = 459295 + 173 = 468173 + 295 = 468183 + 492 = 675以上都是正确的填写法!注意:111 + 222 = 333 是错误的填写法!因为每个数字必须是不同的!也就是说:1~9中的所有数字,每个必须出现且仅出现一次!注意:不包括数字“0”!注意:满足加法交换率的式子算两种不同的答案。所以答案肯定是个偶数!注意:只要求计算不同的填法的数目不要求列出所有填写法更不要求填写源代码!*/package Question10_19;import java.util.Vector;public class Question12 { public static int count; public static void AllType(Vector<Character> sourse,Vector<Character>result) { if(sourse.size()==0){ //System.out.println(result); int a=(result.elementAt(0)-'0')*100+(result.elementAt(1)-'0')*10+result.elementAt(2)-'0'; int b=(result.elementAt(3)-'0')*100+(result.elementAt(4)-'0')*10+result.elementAt(5)-'0'; int c=(result.elementAt(6)-'0')*100+(result.elementAt(7)-'0')*10+result.elementAt(8)-'0'; if(a+b==c){ System.out.printf("%d + %d = %d\n",a,b,c); count++; } }else{ for (int i = 0; i < sourse.size(); i++) { result.add(sourse.elementAt(i)); sourse.remove(i); AllType(sourse, result); sourse.add(i, result.elementAt(result.size()-1)); result.remove(result.size()-1); } } } public static void main(String[] args) { Vector<Character>sourse=new Vector<Character>(); Vector<Character>result=new Vector<Character>(); for (int i = 1; i <= 9; i++) { sourse.add((char)('0'+i)); } AllType(sourse, result); System.out.println(count); }}

运行结果:124 + 659 = 783125 + 739 = 864127 + 359 = 486127 + 368 = 495128 + 367 = 495128 + 439 = 567129 + 357 = 486129 + 438 = 567129 + 654 = 783129 + 735 = 864134 + 658 = 792135 + 729 = 864138 + 429 = 567138 + 654 = 792139 + 428 = 567139 + 725 = 864142 + 596 = 738142 + 695 = 837143 + 586 = 729145 + 692 = 837146 + 583 = 729146 + 592 = 738152 + 487 = 639152 + 784 = 936154 + 629 = 783154 + 638 = 792154 + 782 = 936157 + 329 = 486157 + 482 = 639158 + 634 = 792159 + 327 = 486159 + 624 = 783162 + 387 = 549162 + 783 = 945163 + 782 = 945167 + 328 = 495167 + 382 = 549168 + 327 = 495173 + 286 = 459173 + 295 = 468175 + 293 = 468176 + 283 = 459182 + 367 = 549182 + 394 = 576182 + 457 = 639182 + 493 = 675182 + 754 = 936182 + 763 = 945183 + 276 = 459183 + 492 = 675183 + 546 = 729183 + 762 = 945184 + 392 = 576184 + 752 = 936186 + 273 = 459186 + 543 = 729187 + 362 = 549187 + 452 = 639192 + 384 = 576192 + 483 = 675192 + 546 = 738192 + 645 = 837193 + 275 = 468193 + 482 = 675194 + 382 = 576195 + 273 = 468195 + 642 = 837196 + 542 = 738214 + 569 = 783214 + 659 = 873215 + 478 = 693215 + 748 = 963216 + 378 = 594216 + 738 = 954218 + 349 = 567218 + 376 = 594218 + 439 = 657218 + 475 = 693218 + 736 = 954218 + 745 = 963219 + 348 = 567219 + 438 = 657219 + 564 = 783219 + 654 = 873234 + 657 = 891235 + 746 = 981236 + 718 = 954236 + 745 = 981237 + 654 = 891238 + 419 = 657238 + 716 = 954239 + 418 = 657241 + 596 = 837243 + 576 = 819243 + 675 = 918245 + 673 = 918245 + 718 = 963245 + 736 = 981246 + 573 = 819246 + 591 = 837246 + 735 = 981248 + 319 = 567248 + 715 = 963249 + 318 = 567251 + 397 = 648254 + 619 = 873254 + 637 = 891257 + 391 = 648257 + 634 = 891259 + 614 = 873264 + 519 = 783269 + 514 = 783271 + 593 = 864271 + 683 = 954273 + 186 = 459273 + 195 = 468273 + 546 = 819273 + 591 = 864273 + 645 = 918273 + 681 = 954275 + 193 = 468275 + 418 = 693275 + 643 = 918276 + 183 = 459276 + 318 = 594276 + 543 = 819278 + 316 = 594278 + 415 = 693281 + 394 = 675281 + 673 = 954283 + 176 = 459283 + 671 = 954284 + 391 = 675286 + 173 = 459291 + 357 = 648291 + 384 = 675291 + 546 = 837291 + 573 = 864293 + 175 = 468293 + 571 = 864294 + 381 = 675295 + 173 = 468296 + 541 = 837297 + 351 = 648314 + 658 = 972316 + 278 = 594317 + 529 = 846317 + 628 = 945318 + 249 = 567318 + 276 = 594318 + 627 = 945318 + 654 = 972319 + 248 = 567319 + 527 = 846324 + 567 = 891324 + 657 = 981327 + 159 = 486327 + 168 = 495327 + 519 = 846327 + 564 = 891327 + 618 = 945327 + 654 = 981328 + 167 = 495328 + 617 = 945329 + 157 = 486329 + 517 = 846341 + 586 = 927342 + 576 = 918346 + 572 = 918346 + 581 = 927348 + 219 = 567349 + 218 = 567351 + 297 = 648352 + 467 = 819354 + 618 = 972354 + 627 = 981357 + 129 = 486357 + 291 = 648357 + 462 = 819357 + 624 = 981358 + 614 = 972359 + 127 = 486362 + 187 = 549362 + 457 = 819364 + 527 = 891367 + 128 = 495367 + 182 = 549367 + 452 = 819367 + 524 = 891368 + 127 = 495372 + 546 = 918376 + 218 = 594376 + 542 = 918378 + 216 = 594381 + 294 = 675381 + 546 = 927382 + 167 = 549382 + 194 = 576384 + 192 = 576384 + 291 = 675386 + 541 = 927387 + 162 = 549391 + 257 = 648391 + 284 = 675392 + 184 = 576394 + 182 = 576394 + 281 = 675397 + 251 = 648415 + 278 = 693418 + 239 = 657418 + 275 = 693419 + 238 = 657428 + 139 = 567429 + 138 = 567438 + 129 = 567438 + 219 = 657439 + 128 = 567439 + 218 = 657452 + 187 = 639452 + 367 = 819457 + 182 = 639457 + 362 = 819462 + 357 = 819467 + 352 = 819475 + 218 = 693478 + 215 = 693482 + 157 = 639482 + 193 = 675483 + 192 = 675487 + 152 = 639492 + 183 = 675493 + 182 = 675514 + 269 = 783517 + 329 = 846519 + 264 = 783519 + 327 = 846524 + 367 = 891527 + 319 = 846527 + 364 = 891529 + 317 = 846541 + 296 = 837541 + 386 = 927542 + 196 = 738542 + 376 = 918543 + 186 = 729543 + 276 = 819546 + 183 = 729546 + 192 = 738546 + 273 = 819546 + 291 = 837546 + 372 = 918546 + 381 = 927564 + 219 = 783564 + 327 = 891567 + 324 = 891569 + 214 = 783571 + 293 = 864572 + 346 = 918573 + 246 = 819573 + 291 = 864576 + 243 = 819576 + 342 = 918581 + 346 = 927583 + 146 = 729586 + 143 = 729586 + 341 = 927591 + 246 = 837591 + 273 = 864592 + 146 = 738593 + 271 = 864596 + 142 = 738596 + 241 = 837614 + 259 = 873614 + 358 = 972617 + 328 = 945618 + 327 = 945618 + 354 = 972619 + 254 = 873624 + 159 = 783624 + 357 = 981627 + 318 = 945627 + 354 = 981628 + 317 = 945629 + 154 = 783634 + 158 = 792634 + 257 = 891637 + 254 = 891638 + 154 = 792642 + 195 = 837643 + 275 = 918645 + 192 = 837645 + 273 = 918654 + 129 = 783654 + 138 = 792654 + 219 = 873654 + 237 = 891654 + 318 = 972654 + 327 = 981657 + 234 = 891657 + 324 = 981658 + 134 = 792658 + 314 = 972659 + 124 = 783659 + 214 = 873671 + 283 = 954673 + 245 = 918673 + 281 = 954675 + 243 = 918681 + 273 = 954683 + 271 = 954692 + 145 = 837695 + 142 = 837715 + 248 = 963716 + 238 = 954718 + 236 = 954718 + 245 = 963725 + 139 = 864729 + 135 = 864735 + 129 = 864735 + 246 = 981736 + 218 = 954736 + 245 = 981738 + 216 = 954739 + 125 = 864745 + 218 = 963745 + 236 = 981746 + 235 = 981748 + 215 = 963752 + 184 = 936754 + 182 = 936762 + 183 = 945763 + 182 = 945782 + 154 = 936782 + 163 = 945783 + 162 = 945784 + 152 = 936336

13. 火柴游戏

/*【编程题】(满分34分)这是一个纵横火柴棒游戏。如图[1.jpg],在3x4的格子中,游戏的双方轮流放置火柴棒。其规则是:1. 不能放置在已经放置火柴棒的地方(即只能在空格中放置)。2. 火柴棒的方向只能是竖直或水平放置。3. 火柴棒不能与其它格子中的火柴“连通”。所谓连通是指两根火柴棒可以连成一条直线,且中间没有其它不同方向的火柴“阻拦”。例如:图[1.jpg]所示的局面下,可以在C2位置竖直放置(为了方便描述格子位置,图中左、下都添加了标记),但不能水平放置,因为会与A2连通。同样道理,B2,B3,D2此时两种方向都不可以放置。但如果C2竖直放置后,D2就可以水平放置了,因为不再会与A2连通(受到了C2的阻挡)。4. 游戏双方轮流放置火柴,不可以弃权,也不可以放多根。直到某一方无法继续放置,则该方为负(输的一方)。游戏开始时可能已经放置了多根火柴。你的任务是:编写程序,读入初始状态,计算出对自己最有利的放置方法并输出。如图[1.jpg]的局面表示为:00-1-0000100即用“0”表示空闲位置,用“1”表示竖直放置,用“-”表示水平放置。【输入、输出格式要求】用户先输入整数 n(n<100), 表示接下来将输入 n 种初始局面,每种局面占3行(多个局面间没有空白行)。程序则输出:每种初始局面情况下计算得出的最佳放置法(行号+列号+放置方式)。例如:用户输入:20111-000-0001111----0010则程序可以输出:00-211不难猜出,输出结果的含义为:对第一个局面,在第0行第0列水平放置对第二个局面,在第2行第1列垂直放置注意:行号、列号都是从0开始计数的。对每种局面可能有多个最佳放置方法(解不唯一),只输出一种即可。例如,对第一个局面,001 也是正解;最第二个局面,201也是正解。*/package Question10_19;import java.util.Scanner;public class Question13 { public static boolean isOk(char[][] state, int i, int j) { if (state[i][j] == '-') { for (int j2 = j + 1; j2 < 4; j2++) { if (state[i][j2] == '-') { return false; } else if (state[i][j2] == '1') { return true; } } for (int j2 = j - 1; j2 >= 0; j2--) { if (state[i][j2] == '-') { return false; } else if (state[i][j2] == '1') { return true; } } } else if (state[i][j] == '1') { for (int i2 = i + 1; i2 < 3; i2++) { if (state[i2][j] == '1') { return false; } else if (state[i2][j] == '-') { return true; } } for (int i2 = i - 1; i2 >= 0; i2--) { if (state[i2][j] == '1') { return false; } else if (state[i2][j] == '-') { return true; } } } return true; } private static void jasdklf(char[][] state) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { if (state[i][j] == '0') { state[i][j] = '-'; if (isOk(state, i, j)) { System.out.println(i + "" + j + '-'); return; } state[i][j] = '0'; state[i][j] = '1'; if (isOk(state, i, j)) { System.out.println(i + "" + j + '1'); return; } state[i][j] = '0'; } } } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); scanner.nextLine(); char[][] state = new char[3][4]; String s; while ((n--) > 0) { for (int i = 0; i < 3; i++) { s = scanner.nextLine(); for (int j = 0; j < 4; j++) { state[i][j] = s.charAt(j); } } jasdklf(state); } }}

14. 古代赌局

/* * 【编程题】(满分23分)俗话说:十赌九输。因为大多数赌局的背后都藏有阴谋。不过也不尽然,有些赌局背后藏有的是:“阳谋”。有一种赌局是这样的:桌子上放六个匣子,编号是1至6。多位参与者(以下称玩家)可以把任意数量的钱押在某个编号的匣子上。所有玩家都下注后,庄家同时掷出3个骰子(骰子上的数字都是1至6)。输赢规则如下:1. 若某一个骰子上的数字与玩家所押注的匣子号相同,则玩家拿回自己的押注,庄家按他押注的数目赔付(即1比1的赔率)。2. 若有两个骰子上的数字与玩家所押注的匣子号相同,则玩家拿回自己的押注,庄家按他押注的数目的2倍赔付(即1比2的赔率)。3. 若三个骰子上的数字都与玩家所押注的匣子号相同,则玩家拿回自己的押注,庄家按他押注的数目的6倍赔付(即1比6的赔率)。4. 若玩家所押注匣子号与某个骰子示数乘积等于另外两个骰子示数的乘积,则玩家拿回自己的押注,庄家也不赔付(流局)。5. 若以上规则有同时满足者,玩家可以选择对自己最有利的规则。规则执行后,则庄家收获所有匣子上剩余的押注。乍一看起来,好像规则对玩家有利,庄家吃亏。但经过大量实战,会发现局面很难说,于是怀疑是否庄家做了手脚,庄家则十分爽快地说:可以由玩家提供骰子,甚至也可以由玩家来投掷骰子。你的任务是:通过编程模拟该过程。模拟50万次,假定只有1个玩家,他每次的押注都是1元钱,其押注的匣子号是随机的。再假定庄家有足够的资金用于赔付。最后计算出庄家的盈率(庄家盈利金额/押注总金额)。【输入、输出格式要求】程序无输入,程序运行输出庄家的盈率,四舍五入保留到小数后3位。【注意】请仔细调试!您的程序只有能运行出正确结果的时候才有机会得分!请把所有类写在同一个文件中,调试好后,存入与【考生文件夹】下对应题号的“解答.txt”中即可。相关的工程文件不要拷入。请不要使用package语句。源程序中只能出现JDK1.5中允许的语法或调用。不能使用1.6或更高版本。 */package Question10_19;import java.util.Scanner;public class Question14 { public static void main(String[] args) { int a,b,c,d,sum = 0; for (int i = 0; i < 500000; i++) { a=(int) (Math.random()*6)+1; b=(int) (Math.random()*6)+1; c=(int) (Math.random()*6)+1; d=(int) (Math.random()*6)+1; // System.out.println(a+" "+b+" "+c+" "+d); if(a==b&&a==c&&a==d){ sum-=6; }else if((a==b&&a==c)||(a==c&&a==d)||(a==b&&a==d)){ sum-=2; }else if(a==b||a==c||a==d){ sum-=1; }else if ((a*b==c*d)||(a*c==b*d)||(a*d==b*c)) { sum-=0; }else { sum+=1; } } System.out.printf("%.3f",sum/500000f); }}

程序输出:0.021

15. 源码变换

/* 超文本标记语言(即HTML),是用于描述网页文档的一种标记语言。HTML通过文本来描述文档显示出来应该具有的“样子”。它主要通过标签来定义对象的显示属性或行为。如果把java的源文件直接拷贝到HTML文档中,用浏览器直接打开,会发现本来整齐有序的源文件变成了一团遭。这是因为,文本中的许多回车和空格都被忽略了。而有些符号在html中有特殊的含义,引起了更复杂的局面。为了源文件能正常显示,我们必须为文本加上适当的标签。对特殊的符号进行转义处理。常用的有:HTML 需要转义的实体:& ---> &空格 ---> < ---> <> ---> >" ---> "此外,根据源码的特点,可以把 TAB 转为4个空格来显示。TAB ---> 为了显示为换行,需要在行尾加<br/>标签。为了显示美观,对关键字加粗显示,即在关键字左右加标签。比如:public对单行注释文本用绿色显示,可以使用<font>标签,形如:<font color=green>//这是我的单行注释!</font>注意:如果“//”出现在字符串中,则注意区分,不要错误地变为绿色。不考虑多行注释的问题 /* .... */ /*或*/ /** .... *//*你的任务是:编写程序,把给定的源文件转化为相应的html表达。【输入、输出格式要求】与你的程序同一目录下,存有源文件 a.txt,其中存有标准的java源文件。要求编写程序把它转化为b.html。例如:目前的 a.txt 文件与 b.html 文件就是对应的。可以用记事本打开b.html查看转换后的内容。用浏览器打开b.html则可以看到显示的效果。注意:实际评测的时候使用的a.txt与示例是不同的。 */package Question10_19;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;public class Question15FinishedBefore { private String BoldComments(String tempString, String note) { return null; } public static void main(String[] args) { try { File inFile = new File("test.java"); FileInputStream fileInputStream; fileInputStream = new FileInputStream(inFile); InputStreamReader inputStreamReader = new InputStreamReader( fileInputStream); BufferedReader bufferedReader = new BufferedReader( inputStreamReader); File outFile = new File("test.html"); FileOutputStream fileOutputStream = new FileOutputStream(outFile); OutputStreamWriter outStreamWriter = new OutputStreamWriter( fileOutputStream); BufferedWriter bufferedWriter = new BufferedWriter(outStreamWriter); outStreamWriter.write("<html>\n"); outStreamWriter.write("<body>\n"); String tempString; while ((tempString = bufferedReader.readLine()) != null) { tempString = tempString.replaceAll("&", "&"); tempString = tempString.replaceAll(" ", ""); tempString = tempString.replaceAll("<", "<"); tempString = tempString.replaceAll(">", ">"); int index1 = tempString.lastIndexOf("//"); int index2 = tempString.indexOf("\""); if (index1 != -1 && index2 == -1) { String s1 = tempString.substring(0, index1); String s2 = tempString.substring(index1); s2 = "<font color=green>" + s2 + "</font>"; tempString = s1 + s2; } else if (index1 != -1 && index2 != -1) { int startMark = -1, endMark = -1; boolean isNote = true; for (int i = 0; i < tempString.length(); i++) { if ('\"' == tempString.charAt(i)) { if (startMark == -1) { startMark = i; } else { endMark = i; if (index1 > startMark && index1 < endMark) { isNote = false; break; } else { startMark = -1; endMark = -1; } } } } if (isNote == true) { String s1 = tempString.substring(0, index1); String s2 = tempString.substring(index1); s2 = "<font color=green>" + s2 + "</font>"; tempString = s1 + s2; } } tempString = tempString.replaceAll("\"", """); tempString = tempString.replaceAll("\t", ""); tempString = tempString.replaceAll("public", "public"); tempString = tempString.replaceAll("class", "class"); tempString = tempString.replaceAll("static", "static"); tempString = tempString.replaceAll("void", "void"); outStreamWriter.write(tempString + "<br/>" + "\r\n"); } outStreamWriter.write("\n</body>\n"); outStreamWriter.write("</html>\n"); bufferedWriter.flush(); bufferedReader.close(); bufferedWriter.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}

运行结果:

// 我的工具类public class MyTool{ public static void main(String[] args) { int a = 100; int b = 20; if(a>b && true) System.out.println(a); else System.out.println("this! //aaa//kkk"); // 测试注释显示是否正确 }}

16. 数量周期

/* 复杂现象背后的推动力,可能是极其简单的原理。科学的目标之一就是发现纷繁复杂的自然现象背后的简单法则。爱因斯坦的相对论是这方面的典范例证。很早的时候,生物学家观察某区域某种昆虫的数量(称为虫口数)之逐年变化规律,就十分迷惑:有的时候是逐渐增多达到一个平衡值。有的时候在两个数字间周期跳动。有的时候则进入一片混乱,类似随机数字一样变化(称为混沌现象)。慢慢地,人们从数学中更清晰地观察到了这一现象,并因此开创了:符号动力学、非线性动力学等研究领域。一个著名的虫口数目简化模型如下:x' = x * (1 - x) * r这里,x x' r 都是浮点数。其中,x 表示当年的虫口数,x' 表示下一年的虫口数。它们的取值范围在 0 与 1 之间,实际上表示的是:虫口的总数占环境所能支持的最大数量的比率。r 是常数(环境参数),r的取值范围在 [0,4]。令人惊讶的是:这个简单的迭代公式有着不同寻常的神秘性质!一般来说,多次迭代后,虫口数的稳定模式与x的初始值无关,而与 r 有关!例如:无论x初始值是多少,当 r = 2.5 的时候,x 多次迭代后会趋向于 0.6。而当 r = 3.2 的时候,x 的值会趋向于在 0.799 与 0.513 之间周期性摆动。那么,r = 3.62 的时候,你观察到有什么周期现象发生吗?不需要提交源代码,只要写出你的结论即可!答案写在:“解答.txt”中,不要写在这里。 */public class Demo01 { static int count = 100; // 执行100次退出 public static void f(double x,double r){ if(count<=0) return; x = x * (1 - x) * r; System.out.println(x); count--; f(x,r); } public static void main(String[] args){ double x = 0.2; double r = 3.62; f(x,r); System.out.println("网络某某结论:虫口数目函数呈锯齿状变化," + "虫口数目不存在连续两年增加和连续两年减少的情况。"); }}

运行结果:0.5792000000000002………………0.878401825611548网络某某结论:虫口数目函数呈锯齿状变化,虫口数目不存在连续两年增加和连续两年减少的情况。

17. 提取子串

/*串“abcba”以字母“c”为中心左右对称;串“abba” 是另一种模式的左右对称。这两种情况我们都称这个串是镜像串。特别地,只含有1个字母的串,可以看成是第一种模式的镜像串。一个串可以含有许多镜像子串。我们的目标是求一个串的最大镜像子串(最长的镜像子串),如果有多个最大镜像子串,对称中心靠左的优先选中。例如:“abcdeefghhgfeiieje444k444lmn”的最大镜像子串是:“efghhgfe”下面的静态方法实现了该功能,请仔细阅读并分析代码,填写空白处的代码,使得程序的逻辑合理,结果正确。方法一:*/public class Demo02_two { public static String maxS(String s){ String maxS = ""; char[] c = s.toCharArray(); for(int i=0;i<c.length-1;i++){ int mark = 0; // 下标标记 String temp = ""; // 记录一个镜像串 if(c[i]==c[i+1]){ for(;;){ // abba模式 if((i-mark<0)||c[i-mark]!=c[i+mark+1]) break; mark++; } temp = s.substring(i-mark+1,i+mark+1); }else if((i+2)<c.length&&c[i]==c[i+2]){ for(;;){ // abcba模式 if((i-mark<0)||c[i-mark]!=c[i+mark+2]) break; mark++; } temp = s.substring(i-mark+1,i+mark+2); } if(temp.length()>maxS.length()){ maxS = temp; } } return maxS; } public static void main(String[] args){ String s = "abcdeefghhgfeiieje444k444lmn"; System.out.println(maxS(s)); }}方法二:public class Demo02 { public static String getMaxMirrorString(String s) { String max_s = ""; // 所求的最大对称子串 for (int i = 0; i < s.length(); i++) { // 第一种对称模式 int step = 1; try { for (;;) { if (s.charAt(i - step) != s.charAt(i + step)) break; step++; } } catch (Exception e) { } String s1 = s.substring(i - step + 1, i + step); // 填空1 // 第二种对称模式 step = 0; try { for (;;) { if (s.charAt(i - step) != s.charAt(i + step + 1)) break; // 填空2 step++; } } catch (Exception e) { } String s2 = s.substring(i - step + 1, i + step + 1); if (s1.length() > max_s.length()) max_s = s1; if (s2.length() > max_s.length()) max_s = s2; } return max_s; } public static void main(String[] args) { String s = "abcdeefghhgfeiieje444k444lmn"; System.out.println(getMaxMirrorString(s)); }}

运行结果:efghhgfe

18. 取球游戏

/*今盒子里有n个小球,A、B两人轮流从盒中取球,每个人都可以看到另一个人取了多少个,也可以看到盒中还剩下多少个,并且两人都很聪明,不会做出错误的判断。我们约定:每个人从盒子中取出的球的数目必须是:1,3,7或者8个。轮到某一方取球时不能弃权!A先取球,然后双方交替取球,直到取完。被迫拿到最后一个球的一方为负方(输方)请编程确定出在双方都不判断失误的情况下,对于特定的初始球数,A是否能赢?程序运行时,从标准输入获得数据,其格式如下:先是一个整数n(n<100),表示接下来有n个整数。然后是n个整数,每个占一行(整数<10000),表示初始球数。程序则输出n行,表示A的输赢情况(输为0,赢为1)。例如,用户输入:4121018则程序应该输出:0110 */package Question10_19;import java.util.Scanner; public class Question18Think2MustRemember { public static boolean array[]=new boolean[10020]; public static void main(String[] args) { array[0]=true; for (int i = 1; i < array.length; i++) { array[i]=(i>=8&&!array[i-8])||(i>=7&&!array[i-7])||(i>=3&&!array[i-3])||(i>=1&&!array[i-1]); } Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); int total; scanner.nextLine(); while ((n--)>0) { total=scanner.nextInt(); System.out.println(array[total]?1:0); } }}

运行结果:4121018输出结果:0110

19. 密码发生器

/*在对银行账户等重要权限设置密码的时候,我们常常遇到这样的烦恼:如果为了好记用生日吧,容易被破解,不安全;如果设置不好记的密码,又担心自己也会忘记;如果写在纸上,担心纸张被别人发现或弄丢了...这个程序的任务就是把一串拼音字母转换为6位数字(密码)。我们可以使用任何好记的拼音串(比如名字,王喜明,就写:wangximing)作为输入,程序输出6位数字。变换的过程如下:第一步. 把字符串6个一组折叠起来,比如wangximing则变为:wangximing第二步. 把所有垂直在同一个位置的字符的ascii码值相加,得出6个数字,如上面的例子,则得出:228 202 220 206 120 105第三步. 再把每个数字“缩位”处理:就是把每个位的数字相加,得出的数字如果不是一位数字,就再缩位,直到变成一位数字为止。例如: 228 => 2+2+8=12 => 1+2=3上面的数字缩位后变为:344836, 这就是程序最终的输出结果!要求程序从标准输入接收数据,在标准输出上输出结果。输入格式为:第一行是一个整数n(<100),表示下边有多少输入行,接下来是n行字符串,就是等待变换的字符串。输出格式为:n行变换后的6位密码。例如,输入:5zhangfengwangximingjiujingfaziwoaibeijingtiananmenhaohaoxuexi则输出:772243344836297332716652875843*/package Question10_19;import java.util.Scanner; public class Question19 { public static int simplify(int n) { String s; while (n>=10) { s=n+""; n=0; for (int i = 0; i < s.length(); i++) { n+=s.charAt(i)-'0'; } } return n; } public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); String s; scanner.nextLine(); while((n--)>0){ int array[]=new int[6]; s=scanner.nextLine(); for (int i = 0; i < s.length(); i++) { array[i%6]+=(int)(s.charAt(i)); } for (int i = 0; i < 6; i++) { System.out.print(simplify(array[i])); } System.out.println(); }// System.out.println(simplify(123456789)); }}

运行结果:输入整数n(<100)表示下边有多少输入行:5zhangfengwangximingjiujingfaziwoaibeijingtiananmenhaohaoxuexi772243344836297332716652875843

20. 转方阵

/*对一个方阵转置,就是把原来的行号变列号,原来的列号变行号例如,如下的方阵:1 2 3 45 6 7 89 10 11 1213 14 15 16转置后变为:1 5 9 132 6 10 143 7 11 154 8 12 16但,如果是对该方阵顺时针旋转(不是转置),却是如下结果:13 9 5 114 10 6 215 11 7 316 12 8 4下面的代码实现的功能就是要把一个方阵顺时针旋转。*/public class Demo03 { // 矩阵顺时针旋转 public static void rotation(int[][] n,int [][] m,int i,int j){ int t = j; // 标记最后一行的位置 if(i>=n.length) return; for(int k=0;k<n.length;k++){ m[i][k] = n[j--][i]; // 解决一行 } rotation(n,m,++i,t); // 递归解决下一行 } // 输出矩阵 public static void print(int[][] t){ for(int[] x: t){ for(int y:x){ System.out.print(y+"\t"); } System.out.println(); } } public static void main(String[] args){ int[][] n = { {1 ,2 ,3 ,4 }, {5 ,6 ,7 ,8 }, {9 ,10,11,12}, {13,14,15,16} }; print(n); // 显示原矩阵 int len = n.length; int[][] m = new int[len][len]; // 目标矩阵 rotation(n,m,0,len-1); // 矩阵顺时针旋转 System.out.println("顺时针旋转结果:"); print(m); // 显示目标矩阵 }}

运行结果:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 顺时针旋转结果:13 9 5 1 14 10 6 2 15 11 7 3 16 12 8 4

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: java蓝桥杯历年真题及答案整理(小结)

以上就上有关java蓝桥杯历年真题及答案整理(小结)的全部内容,学步园全面介绍编程技术、操作系统、数据库、web前端技术等内容。

抱歉!评论已关闭.