1. 程式人生 > >算法筆記_208:第六屆藍橋杯軟件類決賽真題(Java語言A組)

算法筆記_208:第六屆藍橋杯軟件類決賽真題(Java語言A組)

boolean style 空格 ima eight jdk1 ++ port 但是

目錄

1 胡同門牌號

2 四階幻方

3 顯示二叉樹

4 穿越雷區

5 切開字符串

6 鋪瓷磚

前言:以下代碼僅供參考,若有錯誤歡迎指正哦~


1 胡同門牌號

標題:胡同門牌號

小明家住在一條胡同裏。胡同裏的門牌號都是連續的正整數,由於歷史原因,最小的號碼並不是從1開始排的。
有一天小明突然發現了有趣的事情:
如果除去小明家不算,胡同裏的其它門牌號加起來,剛好是100!
並且,小明家的門牌號剛好等於胡同裏其它住戶的個數!

請你根據這些信息,推算小明家的門牌號是多少?

請提交該整數,不要填寫任何多余的內容或說明性文字。

運行結果有兩個:8和10,但是針對此題,答案到底是8還是10還是8和10,不好判定。以下代碼僅供參考。
(
1)8 (2)10

 1 public class Main {
 2     
 3     public static void main(String[] args) {
 4         for(int i = 2;i < 200;i++) {
 5             int sum = 0;
 6             int count = 0;
 7             for(int j = i;j < 200;j++) {
 8                 count++;
 9                 sum = sum + j;
10                 if
(sum - count + 1 == 100 && count - 1 >= i) 11 System.out.println("i = "+i+", j = "+j+", count = "+(count-1)); 12 } 13 } 14 } 15 }


2 四階幻方

標題:四階幻方

把1~16的數字填入4x4的方格中,使得行、列以

及兩個對角線的和都相等,滿足這樣的特征時稱

為:四階幻方。

四階幻方可能有很多方案。如果固定左上角為1

,請計算一共有多少種方案。
比如:
  
1 2 15 16 12 14 3 5 13 7 10 4 8 11 6 9 以及: 1 12 13 8 2 14 7 11 15 3 10 6 16 5 4 9 就可以算為兩種不同的方案。 請提交左上角固定為1時的所有方案數字,不要 填寫任何多余內容或說明文字。 答案:416

 1 import java.util.ArrayList;
 2 
 3 public class Main {
 4     public static boolean[] used = new boolean[17];
 5     public static ArrayList<String> list = new ArrayList<String>();
 6     public static int count = 0;
 7     
 8     public boolean check(int[] A, int step) {
 9         if(step >= 4)
10             if(A[0] + A[1] + A[2] + A[3] != 34)
11                 return false;
12         if(step >= 8)
13             if(A[4] + A[5] + A[6] + A[7] != 34)
14                 return false;
15         if(step >= 12)
16             if(A[8] + A[9] + A[10] + A[11] != 34)
17                 return false;
18         if(step >= 13)
19             if(A[0] + A[4] + A[8] + A[12] != 34 || A[3] + A[6] + A[9] + A[12] != 34)
20                 return false;
21         if(step >= 14)
22             if(A[1] + A[5] + A[9] + A[13] != 34)
23                 return false;
24         if(step >= 15)
25             if(A[2] + A[6] + A[10] + A[14] != 34)
26                 return false;
27         if(step >= 16)
28             if(A[3] + A[7] + A[11] + A[15] != 34 || A[0] + A[5] + A[10] + A[15] != 34)
29                 return false;
30         return true;
31     }
32     
33     public void dfs(int[] A, int step) {
34         if(check(A, step) == false)
35             return;
36         if(step == 16) {
37             StringBuffer s = new StringBuffer("");
38             for(int i = 0;i < A.length;i++)
39                 s.append(A[i]);
40             if(!list.contains(s.toString())) {
41                 list.add(s.toString());
42                 count++;
43             }
44             return;
45         }
46         for(int i = 2;i <= 16;i++) {
47             if(used[i] == false) {
48                 used[i] = true;
49                 A[step] = i;
50                 dfs(A, step + 1);
51                 used[i] = false;
52             }
53         }
54     }
55     
56     public static void main(String[] args) {
57         Main test = new Main();
58         int[] A = new int[16];
59         A[0] = 1;
60         used[1] = true;
61         test.dfs(A, 1);
62         System.out.println(count);
63     }
64 }


3 顯示二叉樹

標題:顯示二叉樹

排序二叉樹的特征是:
某個節點的左子樹的所有節點值都不大於本節點

值。
某個節點的右子樹的所有節點值都不小於本節點

值。

為了能形象地觀察二叉樹的建立過程,小明寫了

一段程序來顯示出二叉樹的結構來。


class BiTree
{
    private int v;
    private BiTree l;
    private BiTree r;
    
    public BiTree(int v){
        this.v = v;
    }
    
    public void add(BiTree the){
        if(the.v < v){
            if(l==null) l 

= the;
            else l.add

(the);
        }
        else{
            if(r==null) r 

= the;
            else r.add

(the);
        }
    }
    
    public int getHeight(){
        int h = 2;
        int hl = l==null? 0 : 

l.getHeight();
        int hr = r==null? 0 : 

r.getHeight();
        return h + Math.max

(hl,hr);
    }
    
    public int getWidth(){
        int w = (""+v).length();
        if(l!=null) w += 

l.getWidth();
        if(r!=null) w += 

r.getWidth();
        return w;
    }
    
    public void show(){
        char[][] buf = new char

[getHeight()][getWidth()];
        printInBuf(buf, 0, 0);
        showBuf(buf);
    }
    
    private void showBuf(char[][] x){
        for(int i=0; i<x.length; 

i++){
            for(int j=0; 

j<x[i].length; j++)
                

System.out.print(x[i][j]==0? ‘ ‘:x[i][j]);
            

System.out.println();
        }
    }
    
    private void printInBuf(char[][] 

buf, int x, int y){
        String sv = "" + v;
        
        int p1 = l==null? x : 

l.getRootPos(x);
        int p2 = getRootPos(x);
        int p3 = r==null? p2 : 

r.getRootPos(p2+sv.length());
        
        buf[y][p2] = ‘|‘;
        for(int i=p1; i<=p3; i+

+) buf[y+1][i]=‘-‘;
        for(int i=0; i<sv.length

(); i++) ________________________________;  //填空

位置
        if(p1<p2) buf[y+1][p1] 

= ‘/‘;
        if(p3>p2) buf[y+1][p3] 

= ‘\\‘;
        
        if(l!=null) l.printInBuf

(buf,x,y+2);
        if(r!=null) r.printInBuf

(buf,p2+sv.length(),y+2);
    }
    
    private int getRootPos(int x){
        return l==null? x : x + 

l.getWidth();
    }
}

public class Main
{
    public static void main(String[] 

args)
    {        
        BiTree tree = new 

BiTree(500);
        tree.add(new BiTree

(200));
        tree.add(new BiTree

(509));
        tree.add(new BiTree

(100));
        tree.add(new BiTree

(250));
        tree.add(new BiTree

(507));
        tree.add(new BiTree

(600));
        tree.add(new BiTree

(650));
        tree.add(new BiTree

(450));
        tree.add(new BiTree

(510));
        tree.add(new BiTree

(440));
        tree.add(new BiTree

(220));        
        tree.show();        
    }
}

對於上邊的測試數據,應該顯示出:
                  |
   /--------------500---   |                    |
/--200---\           /--509---|        |           |        |
100   /--250---\     507   /--600      |        |           |     |
      220   /--450         510   650
            |
            440

(如有對齊問題,請參考【圖1.png】)

請分析程序邏輯,填寫劃線部分缺失的代碼。

註意,只填寫缺少的部分,不要填寫已有的代碼

或符號,也不要加任何說明文字。


答案:buf[y+1][p2+i] = sv.charAt(i)


4 穿越雷區

標題:穿越雷區

X星的坦克戰車很奇怪,它必須交替地穿越正能量輻射區和負能量輻射區才能保持正常運轉,否則將報廢。
某坦克需要從A區到B區去(A,B區本身是安全區,沒有正能量或負能量特征),怎樣走才能路徑最短?

已知的地圖是一個方陣,上面用字母標出了A,B區,其它區都標了正號或負號分別表示正負能量輻射區。
例如:
A + - + -
- + - - +
- + + + -
+ - + - +
B + - + -

坦克車只能水平或垂直方向上移動到相鄰的區。

數據格式要求:

輸入第一行是一個整數n,表示方陣的大小, 4<=n<100
接下來是n行,每行有n個數據,可能是A,B,+,-中的某一個,中間用空格分開。
A,B都只出現一次。

要求輸出一個整數,表示坦克從A區到B區的最少移動步數。
如果沒有方案,則輸出-1

例如:
用戶輸入:
5
A + - + -
- + - - +
- + + + -
+ - + - +
B + - + -

則程序應該輸出:
10

資源約定:
峰值內存消耗(含虛擬機) < 512M
CPU消耗  < 2000ms


請嚴格按要求輸出,不要畫蛇添足地打印類似:“請您輸入...” 的多余內容。

所有代碼放在同一個源文件中,調試通過後,拷貝提交該源碼。
註意:不要使用package語句。不要使用jdk1.7及以上版本的特性。
註意:主類的名字必須是:Main,否則按無效代碼處理。

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static int min = 1000 * 1000;
 5     public static int n;
 6     public static String[][] map;
 7     public static int[][] move = {{-1,0},{1,0},{0,-1},{0,1}}; //表示分別向上、下、左、右移動一步
 8     
 9     public void dfs(boolean[][] visited, int x, int y, int count) {
10         if(map[x][y].equals("B")) {
11             min = Math.min(min, count);
12             return;
13         }
14         for(int i = 0;i < 4;i++) {
15             int tempX = x + move[i][0];
16             int tempY = y + move[i][1];
17             if(tempX >= 0 && tempX < n && tempY >= 0 && tempY < n) {
18                 if(!map[x][y].equals(map[tempX][tempY]) && !visited[tempX][tempY]) {
19                     visited[tempX][tempY] = true;
20                     dfs(visited, tempX, tempY, count + 1);
21                     visited[tempX][tempY] = false;
22                 }
23             }
24         }
25     }
26     
27     public void getResult() {
28         int startX = 0, startY = 0;
29         for(int i = 0;i < n;i++)
30             for(int j = 0;j < n;j++)
31                 if(map[i][j] == "A") {
32                     startX = i;
33                     startY = j;
34                 }
35         boolean[][] visited = new boolean[n][n];
36         visited[startX][startY] = true;
37         dfs(visited, startX, startY, 0);
38         if(min == 1000 * 1000)
39             System.out.println("-1");
40         else
41             System.out.println(min);
42     }
43     
44     public static void main(String[] args) {
45         Main test = new Main();
46         Scanner in = new Scanner(System.in);
47         n = in.nextInt();
48         in.nextLine();
49         String[] T = new String[n];
50         map = new String[n][n];
51         for(int i = 0;i < n;i++) {
52             T[i] = in.nextLine();
53             map[i] = T[i].split(" ");
54         }
55         test.getResult();
56     }
57 }


5 切開字符串

標題:切開字符串

Pear有一個字符串,不過他希望把它切成兩段。
這是一個長度為N(<=10^5)的字符串。
Pear希望選擇一個位置,把字符串不重復不遺漏地切成兩段,長度分別是t和N-t(這兩段都必須非空)。

Pear用如下方式評估切割的方案:
定義“正回文子串”為:長度為奇數的回文子串。
設切成的兩段字符串中,前一段中有A個不相同的正回文子串,後一段中有B個不相同的非正回文子串,則該方案的得分為A*B。

註意,後一段中的B表示的是:“...非正回文...”,而不是: “...正回文...”。
那麽所有的切割方案中,A*B的最大值是多少呢?

【輸入數據】
輸入第一行一個正整數N(<=10^5)
接下來一行一個字符串,長度為N。該字符串僅包含小寫英文字母。
【輸出數據】
一行一個正整數,表示所求的A*B的最大值。
【樣例輸入】
10
bbaaabcaba
【樣例輸出】
38
【數據範圍】
對於20%的數據,N<=100
對於40%的數據,N<=1000
對於100%的數據,N<=10^5

資源約定:
峰值內存消耗(含虛擬機) < 512M
CPU消耗  < 2000ms


請嚴格按要求輸出,不要畫蛇添足地打印類似:“請您輸入...” 的多余內容。

所有代碼放在同一個源文件中,調試通過後,拷貝提交該源碼。
註意:不要使用package語句。不要使用jdk1.7及以上版本的特性。
註意:主類的名字必須是:Main,否則按無效代碼處理。


以下代碼對於N大於10000可能會超時,代碼僅供參考。

import java.util.HashSet;
import java.util.Scanner;

public class Main {
    public static String A, S;
    
    public int Manacher(String M, String N) {
        int count = 0;  //統計字符串M中不同正回文串個數
        int[] P = new int[M.length()];
        int mx = 0, id = 0;
        for(int i = 1;i < M.length();i++) {
            if(mx > i)
                P[i] = Math.min(P[2 * id - i], mx - i);
            else
                P[i] = 1;
            while(i+P[i] < M.length() && i-P[i] >= 0 && 
                    M.charAt(i+P[i]) == M.charAt(i-P[i]))
                P[i]++;
            if(P[i] + i > mx) {
                mx = i + P[i];
                id = i;
            }
        }
        HashSet<String> set = new HashSet<String>();
        for(int i = 2;i < P.length;i = i + 2) {
            int len = P[i] - 1;
            int j = i / 2 - 1;  //回文串中心字符在字符串N中的位置
            if(len == 0) {
                String s = N.substring(j, j + 1);
                if(!set.contains(s)) {
                    set.add(s);
                    count++;
                }
            } else if(len % 2 == 1){
                for(int k = 0;k <= len / 2;k++) {
                    String s = N.substring(j - k, j + k + 1);
                    if(!set.contains(s)) {
                        set.add(s);
                        count++;
                    }
                }
            }
        }
        return count;
    }
    
    public int getAllChildern(String M) {
        int count = 0;  //統計字符串M所有不同子串個數
        HashSet<String> set = new HashSet<String>();
        for(int i = 0, len = 1;i < M.length();i++, len++) {
            for(int j = 0;j <= M.length() - len;j++) {
                String s = M.substring(j, j + len);
                if(!set.contains(s)) {
                    set.add(s);
                    count++;
                }
            }
        }
        return count;
    }
    
    public void getResult() {
        int max = -1;
        for(int i = 1;i < A.length() - 1;i++) {
            String s1 = S.substring(0, i * 2 + 1);
            String s2 = "$" + S.substring(i * 2 + 1);
            String s3 = A.substring(0, i);
            String s4 = A.substring(i);
            int a = Manacher(s1, s3);
            int b = getAllChildern(s4) - Manacher(s2, s4);
            max = Math.max(max, a * b);
        }
        System.out.println(max);
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        Scanner in = new Scanner(System.in);
        A = in.next();
        StringBuilder s1 = new StringBuilder("$#");
        for(int i = 0;i < A.length();i++) {
            s1.append(A.charAt(i));
            s1.append("#");
        }
        S = s1.toString();
        test.getResult();
    }
}


6 鋪瓷磚

標題:鋪瓷磚

為了讓藍橋杯競賽更順利的進行,主辦方決定給競賽的機房重新鋪放瓷磚。機房可以看成一個n*m的矩形,而這次使用的瓷磚比較特別,有兩種形狀,如【圖1.png】所示。在鋪放瓷磚時,可以旋轉。
 
主辦方想知道,如果使用這兩種瓷磚把機房鋪滿,有多少種方案。

【輸入格式】
輸入的第一行包含兩個整數,分別表示機房兩個方向的長度。

【輸出格式】
輸出一個整數,表示可行的方案數。這個數可能很大,請輸出這個數除以65521的余數。

【樣例輸入1】
4 4
【樣例輸出1】
2
【樣例說明1】
這兩種方案如下【圖2.png】所示:
 
【樣例輸入2】
2 6
【樣例輸出2】
4
【數據規模與約定】
對於20%的數據,1<=n, m<=5。
對於50%的數據,1<=n<=100,1<=m<=5。
對於100%的數據,1<=n<=10^15,1<=m<=6。
 
資源約定:
峰值內存消耗(含虛擬機) < 512M
CPU消耗  < 8000ms


請嚴格按要求輸出,不要畫蛇添足地打印類似:“請您輸入...” 的多余內容。

所有代碼放在同一個源文件中,調試通過後,拷貝提交該源碼。
註意:不要使用package語句。不要使用jdk1.7及以上版本的特性。
註意:主類的名字必須是:Main,否則按無效代碼處理。

技術分享

圖1

技術分享

圖2

問題待解決>~<

算法筆記_208:第六屆藍橋杯軟件類決賽真題(Java語言A組)