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 号) 在优先缓存中。
模拟

原文地址:https://www.cnblogs.com/whisperbb/p/12405042.html