2019第十屆藍橋杯JAVA省賽B組


B 不同子串

題目描述

        一個字符串的非空子串是指字符串中長度至少為 1 的連續的一段字符組成 的串。例如,字符串aaab 有非空子串a, b, aa, ab, aaa, aab, aaab,一共 7 個。 注意在計算時,只算本質不同的串的個數。

       請問,字符串0100110001010001 有多少個不同的非空子串?

答案:100

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

public class B {

    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        Scanner in = new Scanner(System.in);
        String str = in.next();
        for(int i = 0; i < str.length(); i++) {
            for(int j = i; j < str.length(); j++) {
                set.add(str.substring(i, j + 1));
            }
        }
        System.out.println(set.size());
    }

}

試題 C: 數列求值  
【問題描述】 給定數列 1, 1, 1, 3, 5, 9, 17, …,從第 4 項開始,每項都是前 3 項的和。求 第 20190324 項的最后 4 位數字。

【答案提交】 這是一道結果填空的題,你只需要算出結果后提交即可。本題的結果為一 個 4 位整數(提示:答案的千位不為 0),在提交答案時只填寫這個整數,填寫 多余的內容將無法得分。
答案: 4659

public class C {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] arr = new int[20190325];
        arr[1] = 1;
        arr[2] = 1;
        arr[3] = 1;
        for(int i = 4; i <= 20190324; i++) {
            arr[i] = (arr[i - 1] + arr[i - 2] + arr[i - 3]) % 10000;
        }
        System.out.println(arr[20190324]);
    }
}

試題 D: 數的分解 
【問題描述】 把 2019 分解成 3 個各不相同的正整數之和,並且要求每個正整數都不包 含數字 2 和 4,一共有多少種不同的分解方法? 注意交換 3 個整數的順序被視為同一種方法,例如 1000+1001+18 和 1001+1000+18 被視為同一種。
答案: 40785

暴力匹配 ,要跑一會

public class D {

    public static void main(String[] args) {
        int count = 0;
        for(int i = 1; i <= 2019; i++) {
            for(int j = i + 1; j <= 2019; j++) {
                for(int k = j + 1; k <= 2019; k++) {
                    String str = String.valueOf(i);
                    String str1 = String.valueOf(j);
                    String str2 = String.valueOf(k);
                    if((i + j + k == 2019) && !str.contains("2") &&
                            !str1.contains("2") && !str2.contains("2") &&
                            !str.contains("4") &&!str1.contains("4") &&
                            !str2.contains("4")
                            )
                        count++;
                }
            }
        }
        System.out.println(count);
    }
}

試題 E: 迷宮
【問題描述】 下圖給出了一個迷宮的平面圖,其中標記為 1 的為障礙,標記為 0 的為可 以通行的地方。 010000 000100 001001 110000 迷宮的入口為左上角,出口為右下角,在迷宮中,只能從一個位置走到這 個它的上、下、左、右四個方向之一。 對於上面的迷宮,從入口開始,可以按DRRURRDDDR 的順序通過迷宮, 一共 10 步。其中 D、U、L、R 分別表示向下、向上、向左、向右走。 對於下面這個更復雜的迷宮(30 行 50 列),請找出一種通過迷宮的方式, 其使用的步數最少,在步數最少的前提下,請找出字典序最小的一個作為答案。 請注意在字典序中D<L<R<U.

 

 

01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000

 

import java.io.BufferedInputStream;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class E {
    static int[][] dir = new int[][]{{1,0},{0,-1},{0,1},{-1,0}};
    static String[][] ans = new String[30][50]; //方向
    static char[][] arr = new char[30][50];
    static int[][] len = new int[30][50]; //長度
    static int[][] vis = new int[30][50];
    public static void bfs(int x, int y) {
        Queue<Node> queue = new LinkedList<>();
        queue.add(new Node(x, y));
        vis[x][y] = 1;
        ans[0][0] = "";
        while(!queue.isEmpty()) {
            Node node = queue.poll();
            for(int i = 0; i < 4; i++) {
                int newx = node.x + dir[i][0];
                int newy = node.y + dir[i][1];
                if(newx >= 0 && newx < 30 && newy >= 0 && newy < 50 && vis[newx][newy] == 0 && arr[newx][newy] == '0') {
                    vis[newx][newy] = 1;
                    len[newx][newy] = len[node.x][node.y] + 1;
                    ans[newx][newy] = ans[node.x][node.y] + String.valueOf(i);
                    queue.add(new Node(newx, newy));
                }
            }
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        
        for(int i = 0; i < 30; i++) {
            String str = in.next();
            for(int j = 0; j < 50; j++) {
                arr[i][j] = str.charAt(j);
            }
        }
        bfs(0, 0);
        
        String st = ans[29][49];
        System.out.println(len[29][49]);
        //System.out.println(st.charAt(5));
        for(int i = 0; i < st.length(); i++) {
            char c = st.charAt(i);
            if(c == '0') {
                System.out.print("D");
            }else if(c == '1') {
                System.out.print("L");
            }else if(c == '2') {
                System.out.print("R");
            }else if(c == '3') {
                System.out.print("U");
            }
        }
    }
}

class Node{
    int x;
    int y;
    public Node(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }
    
}

試題  F:特別數的和
題目描述

小明對數位中含有 2、0、1、9 的數字很感興趣(不包括前導 0),在 1 到 40 中這樣的數包括 1、2、9、10 至 32、39 和 40,共 28 個,他們的和是 574。

請問,在 1 到 n 中,所有這樣的數的和是多少?

【輸入格式】

輸入一行包含兩個整數 n。

【輸出格式】

        輸出一行,包含一個整數,表示滿足條件的數的和。

【樣例輸入】

40

【樣例輸出】

574

import java.util.Scanner;
public class F {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int count = 0;
        for(int i = 1; i <= n; i++) {
            String str = String.valueOf(i);
            if(str.contains("1") || str.contains("2") || str.contains("9") || str.contains("0")) {
                count += i;
            }
        }
        System.out.println(count);
    }
}

試題 G:外賣店優先級
題目描述

“飽了么”外賣系統中維護着 N 家外賣店,編號 1 ∼ N。每家外賣店都有 一個優先級,初始時 (0 時刻) 優先級都為 0。

每經過 1 個時間單位,如果外賣店沒有訂單,則優先級會減少 1,最低減 到 0;而如果外賣店有訂單,則優先級不減反加,每有一單優先級加 2。

如果某家外賣店某時刻優先級大於 5,則會被系統加入優先緩存中;如果 優先級小於等於 3,則會被清除出優先緩存。

給定 T 時刻以內的 M 條訂單信息,請你計算 T 時刻時有多少外賣店在優 先緩存中。

【輸入格式】

 第一行包含 3 個整數 N、M 和 T。
以下 M 行每行包含兩個整數 ts 和 id,表示 ts 時刻編號 id 的外賣店收到 一個訂單。

【輸出格式】

輸出一個整數代表答案。

【樣例輸入】 

2 6 6 
1 1 
5 2 
3 1 
6 2 
2 1 
6 2

【樣例輸出】 

1

【樣例解釋】 

6 時刻時,1 號店優先級降到 3,被移除出優先緩存;2 號店優先級升到 6, 加入優先緩存。所以是有 1 家店 (2 號) 在優先緩存中。
模擬

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM