藍橋杯java歷年真題及答案整理1~20.md

藍橋杯java歷年真題及答案整理(閉關一個月,嘔心瀝血整理出來的)html

1 全排列

是這樣的,若是給定N個不一樣字符,將這N個字符全排列,最終的結果將會是N!種。如:給定 A、B、C三個不一樣的字符,則結果爲:ABC、ACB、BAC、BCA、CAB、CBA一共3!=3*2=6種狀況。java

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
.....
請編程,輸出全部可能的狀況!
注意:
左邊的兩個乘數交換算同一方案,不要重複輸出!
不一樣方案的輸出順序不重要dom

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
相關文章
相關標籤/搜索