温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java蓝桥杯真题的示例分析

发布时间:2021-08-23 10:27:40 来源:亿速云 阅读:134 作者:小新 栏目:编程语言

小编给大家分享一下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. 把每个单词的首字母变为大写。

  2. 把数字与字母之间用下划线字符(_)分开,使得更清晰

  3. 把单词中间有多个空格的调整为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 cpp2005program
You And Me What Cpp_2005_program

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 {
  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 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]);//获取行数和列数
      /*
       * 对列数进行变换
       */
      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)
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;
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);
    }
  }
}

运行结果:
14
3045
0211
…..
…..
……
8
5

6. 括号问题

下面的代码用于判断一个串中的括号是否匹配 所谓匹配是指不同类型的括号必须左右呼应,可以相互包含,但不能交叉
例如:
..(..[..]..).. 是允许的
..(...[...)....].... 是禁止的
对于 main 方法中的测试用例,应该输出:
false
true
false
false
请分析代码逻辑,并推测划线处的代码。
答案写在 “解答.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("...(..[...].(.).){.(..)...."));
  }
}

运行结果:
false
true
false
false

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" 则返回2
s = "82445adb5" 则返回8
s = "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));
  }
}

运行结果:
2
8
-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+"秒"); // 运行总时
  }
}

运行结果:
128468643043731391252
449177399146038697307

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 = 459
295 + 173 = 468
173 + 295 = 468
183 + 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 = 783
125 + 739 = 864
127 + 359 = 486
127 + 368 = 495
128 + 367 = 495
128 + 439 = 567
129 + 357 = 486
129 + 438 = 567
129 + 654 = 783
129 + 735 = 864
134 + 658 = 792
135 + 729 = 864
138 + 429 = 567
138 + 654 = 792
139 + 428 = 567
139 + 725 = 864
142 + 596 = 738
142 + 695 = 837
143 + 586 = 729
145 + 692 = 837
146 + 583 = 729
146 + 592 = 738
152 + 487 = 639
152 + 784 = 936
154 + 629 = 783
154 + 638 = 792
154 + 782 = 936
157 + 329 = 486
157 + 482 = 639
158 + 634 = 792
159 + 327 = 486
159 + 624 = 783
162 + 387 = 549
162 + 783 = 945
163 + 782 = 945
167 + 328 = 495
167 + 382 = 549
168 + 327 = 495
173 + 286 = 459
173 + 295 = 468
175 + 293 = 468
176 + 283 = 459
182 + 367 = 549
182 + 394 = 576
182 + 457 = 639
182 + 493 = 675
182 + 754 = 936
182 + 763 = 945
183 + 276 = 459
183 + 492 = 675
183 + 546 = 729
183 + 762 = 945
184 + 392 = 576
184 + 752 = 936
186 + 273 = 459
186 + 543 = 729
187 + 362 = 549
187 + 452 = 639
192 + 384 = 576
192 + 483 = 675
192 + 546 = 738
192 + 645 = 837
193 + 275 = 468
193 + 482 = 675
194 + 382 = 576
195 + 273 = 468
195 + 642 = 837
196 + 542 = 738
214 + 569 = 783
214 + 659 = 873
215 + 478 = 693
215 + 748 = 963
216 + 378 = 594
216 + 738 = 954
218 + 349 = 567
218 + 376 = 594
218 + 439 = 657
218 + 475 = 693
218 + 736 = 954
218 + 745 = 963
219 + 348 = 567
219 + 438 = 657
219 + 564 = 783
219 + 654 = 873
234 + 657 = 891
235 + 746 = 981
236 + 718 = 954
236 + 745 = 981
237 + 654 = 891
238 + 419 = 657
238 + 716 = 954
239 + 418 = 657
241 + 596 = 837
243 + 576 = 819
243 + 675 = 918
245 + 673 = 918
245 + 718 = 963
245 + 736 = 981
246 + 573 = 819
246 + 591 = 837
246 + 735 = 981
248 + 319 = 567
248 + 715 = 963
249 + 318 = 567
251 + 397 = 648
254 + 619 = 873
254 + 637 = 891
257 + 391 = 648
257 + 634 = 891
259 + 614 = 873
264 + 519 = 783
269 + 514 = 783
271 + 593 = 864
271 + 683 = 954
273 + 186 = 459
273 + 195 = 468
273 + 546 = 819
273 + 591 = 864
273 + 645 = 918
273 + 681 = 954
275 + 193 = 468
275 + 418 = 693
275 + 643 = 918
276 + 183 = 459
276 + 318 = 594
276 + 543 = 819
278 + 316 = 594
278 + 415 = 693
281 + 394 = 675
281 + 673 = 954
283 + 176 = 459
283 + 671 = 954
284 + 391 = 675
286 + 173 = 459
291 + 357 = 648
291 + 384 = 675
291 + 546 = 837
291 + 573 = 864
293 + 175 = 468
293 + 571 = 864
294 + 381 = 675
295 + 173 = 468
296 + 541 = 837
297 + 351 = 648
314 + 658 = 972
316 + 278 = 594
317 + 529 = 846
317 + 628 = 945
318 + 249 = 567
318 + 276 = 594
318 + 627 = 945
318 + 654 = 972
319 + 248 = 567
319 + 527 = 846
324 + 567 = 891
324 + 657 = 981
327 + 159 = 486
327 + 168 = 495
327 + 519 = 846
327 + 564 = 891
327 + 618 = 945
327 + 654 = 981
328 + 167 = 495
328 + 617 = 945
329 + 157 = 486
329 + 517 = 846
341 + 586 = 927
342 + 576 = 918
346 + 572 = 918
346 + 581 = 927
348 + 219 = 567
349 + 218 = 567
351 + 297 = 648
352 + 467 = 819
354 + 618 = 972
354 + 627 = 981
357 + 129 = 486
357 + 291 = 648
357 + 462 = 819
357 + 624 = 981
358 + 614 = 972
359 + 127 = 486
362 + 187 = 549
362 + 457 = 819
364 + 527 = 891
367 + 128 = 495
367 + 182 = 549
367 + 452 = 819
367 + 524 = 891
368 + 127 = 495
372 + 546 = 918
376 + 218 = 594
376 + 542 = 918
378 + 216 = 594
381 + 294 = 675
381 + 546 = 927
382 + 167 = 549
382 + 194 = 576
384 + 192 = 576
384 + 291 = 675
386 + 541 = 927
387 + 162 = 549
391 + 257 = 648
391 + 284 = 675
392 + 184 = 576
394 + 182 = 576
394 + 281 = 675
397 + 251 = 648
415 + 278 = 693
418 + 239 = 657
418 + 275 = 693
419 + 238 = 657
428 + 139 = 567
429 + 138 = 567
438 + 129 = 567
438 + 219 = 657
439 + 128 = 567
439 + 218 = 657
452 + 187 = 639
452 + 367 = 819
457 + 182 = 639
457 + 362 = 819
462 + 357 = 819
467 + 352 = 819
475 + 218 = 693
478 + 215 = 693
482 + 157 = 639
482 + 193 = 675
483 + 192 = 675
487 + 152 = 639
492 + 183 = 675
493 + 182 = 675
514 + 269 = 783
517 + 329 = 846
519 + 264 = 783
519 + 327 = 846
524 + 367 = 891
527 + 319 = 846
527 + 364 = 891
529 + 317 = 846
541 + 296 = 837
541 + 386 = 927
542 + 196 = 738
542 + 376 = 918
543 + 186 = 729
543 + 276 = 819
546 + 183 = 729
546 + 192 = 738
546 + 273 = 819
546 + 291 = 837
546 + 372 = 918
546 + 381 = 927
564 + 219 = 783
564 + 327 = 891
567 + 324 = 891
569 + 214 = 783
571 + 293 = 864
572 + 346 = 918
573 + 246 = 819
573 + 291 = 864
576 + 243 = 819
576 + 342 = 918
581 + 346 = 927
583 + 146 = 729
586 + 143 = 729
586 + 341 = 927
591 + 246 = 837
591 + 273 = 864
592 + 146 = 738
593 + 271 = 864
596 + 142 = 738
596 + 241 = 837
614 + 259 = 873
614 + 358 = 972
617 + 328 = 945
618 + 327 = 945
618 + 354 = 972
619 + 254 = 873
624 + 159 = 783
624 + 357 = 981
627 + 318 = 945
627 + 354 = 981
628 + 317 = 945
629 + 154 = 783
634 + 158 = 792
634 + 257 = 891
637 + 254 = 891
638 + 154 = 792
642 + 195 = 837
643 + 275 = 918
645 + 192 = 837
645 + 273 = 918
654 + 129 = 783
654 + 138 = 792
654 + 219 = 873
654 + 237 = 891
654 + 318 = 972
654 + 327 = 981
657 + 234 = 891
657 + 324 = 981
658 + 134 = 792
658 + 314 = 972
659 + 124 = 783
659 + 214 = 873
671 + 283 = 954
673 + 245 = 918
673 + 281 = 954
675 + 243 = 918
681 + 273 = 954
683 + 271 = 954
692 + 145 = 837
695 + 142 = 837
715 + 248 = 963
716 + 238 = 954
718 + 236 = 954
718 + 245 = 963
725 + 139 = 864
729 + 135 = 864
735 + 129 = 864
735 + 246 = 981
736 + 218 = 954
736 + 245 = 981
738 + 216 = 954
739 + 125 = 864
745 + 218 = 963
745 + 236 = 981
746 + 235 = 981
748 + 215 = 963
752 + 184 = 936
754 + 182 = 936
762 + 183 = 945
763 + 182 = 945
782 + 154 = 936
782 + 163 = 945
783 + 162 = 945
784 + 152 = 936
336

13. 火柴游戏

/*
【编程题】(满分34分)
这是一个纵横火柴棒游戏。如图[1.jpg],在3x4的格子中,游戏的双方轮流放置火柴棒。其规则是:
1. 不能放置在已经放置火柴棒的地方(即只能在空格中放置)。
2. 火柴棒的方向只能是竖直或水平放置。
3. 火柴棒不能与其它格子中的火柴“连通”。所谓连通是指两根火柴棒可以连成一条直线,
且中间没有其它不同方向的火柴“阻拦”。
例如:图[1.jpg]所示的局面下,可以在C2位置竖直放置(为了方便描述格子位置,图中左、下都添加了标记),
但不能水平放置,因为会与A2连通。同样道理,B2,B3,D2此时两种方向都不可以放置。但如果C2竖直放置后,
D2就可以水平放置了,因为不再会与A2连通(受到了C2的阻挡)。
4. 游戏双方轮流放置火柴,不可以弃权,也不可以放多根。直到某一方无法继续放置,则该方为负(输的一方)。
游戏开始时可能已经放置了多根火柴。
你的任务是:编写程序,读入初始状态,计算出对自己最有利的放置方法并输出。
如图[1.jpg]的局面表示为:
00-1
-000
0100
即用“0”表示空闲位置,用“1”表示竖直放置,用“-”表示水平放置。
【输入、输出格式要求】
用户先输入整数 n(n<100), 表示接下来将输入 n 种初始局面,每种局面占3行(多个局面间没有空白行)。
程序则输出:每种初始局面情况下计算得出的最佳放置法(行号+列号+放置方式)。
例如:用户输入:
2
0111
-000
-000
1111
----
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 需要转义的实体:
&   ---> &
空格     ---> &nbsp;
<   ---> <
>   ---> >
"   ---> "
此外,根据源码的特点,可以把 TAB 转为4个空格来显示。
TAB  ---> &nbsp;&nbsp;&nbsp;&nbsp;
为了显示为换行,需要在行尾加<br/>标签。
为了显示美观,对关键字加粗显示,即在关键字左右加<b>标签。比如:
<b>public</b>
对单行注释文本用绿色显示,可以使用<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("&", "&amp;");
        tempString = tempString.replaceAll(" ", "&nbsp;");
        tempString = tempString.replaceAll("<", "&lt;");
        tempString = tempString.replaceAll(">", "&gt;");

        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("\"", "&quot;");
        tempString = tempString.replaceAll("\t",
            "&nbsp;&nbsp;&nbsp;&nbsp;");

        tempString = tempString.replaceAll("&nbsp;public&nbsp;",
            "&nbsp;<b>public</b>&nbsp;");
        tempString = tempString.replaceAll("&nbsp;class&nbsp;",
            "&nbsp;<b>class</b>&nbsp;");
        tempString = tempString.replaceAll("&nbsp;static&nbsp;",
            "&nbsp;<b>static</b>&nbsp;");
        tempString = tempString.replaceAll("&nbsp;void&nbsp;",
            "&nbsp;<b>void</b>&nbsp;");

        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)。
例如,用户输入:
4
1
2
10
18
则程序应该输出:
0
1
1
0
 */
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);
    }
  }
}

运行结果:
4
1
2
10
18
输出结果:
0
1
1
0

19. 密码发生器

/*
在对银行账户等重要权限设置密码的时候,我们常常遇到这样的烦恼:如果为了好记用生日吧,
容易被破解,不安全;如果设置不好记的密码,又担心自己也会忘记;如果写在纸上,担心纸张被别人发现或弄丢了...
这个程序的任务就是把一串拼音字母转换为6位数字(密码)。
我们可以使用任何好记的拼音串(比如名字,王喜明,就写:wangximing)作为输入,程序输出6位数字。
变换的过程如下:
第一步. 把字符串6个一组折叠起来,比如wangximing则变为:
wangxi
ming
第二步. 把所有垂直在同一个位置的字符的ascii码值相加,得出6个数字,如上面的例子,则得出:
228 202 220 206 120 105
第三步. 再把每个数字“缩位”处理:就是把每个位的数字相加,得出的数字如果不是一位数字,
就再缩位,直到变成一位数字为止。例如: 228 => 2+2+8=12 => 1+2=3
上面的数字缩位后变为:344836, 这就是程序最终的输出结果!
要求程序从标准输入接收数据,在标准输出上输出结果。
输入格式为:第一行是一个整数n(<100),表示下边有多少输入行,接下来是n行字符串,就是等待变换的字符串。
输出格式为:n行变换后的6位密码。
例如,输入:
5
zhangfeng
wangximing
jiujingfazi
woaibeijingtiananmen
haohaoxuexi
则输出:
772243
344836
297332
716652
875843
*/

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)表示下边有多少输入行:
5
zhangfeng
wangximing
jiujingfazi
woaibeijingtiananmen
haohaoxuexi
772243
344836
297332
716652
875843

20. 转方阵

/*
对一个方阵转置,就是把原来的行号变列号,原来的列号变行号
例如,如下的方阵:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
转置后变为:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
但,如果是对该方阵顺时针旋转(不是转置),却是如下结果:
13 9 5 1
14 10 6 2
15 11 7 3
16 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蓝桥杯真题的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI