Explanation of the 10th (2019) Blue Bridge Cup JavaB Group Exercises

Explanation of the 10th (2019) Blue Bridge Cup JavaB Group Exercises

**

Team 1

**
As a basketball team coach, you need to select one player
from position 1 to position 5 from the following list to form the team's starting lineup.
The scores for each player from position 1 to position 5 are shown in the table below. Would you please calculate the
maximum possible sum of the scores from the 1st to the 5th of the starting lineup ?
Insert picture description here
Solution: Just look at the table number for this question. No code is required. The point is that each team member's score can only be selected once, and many participants did not see the question clearly, leading to the loss of the
number of points: 98 ( Number 17) Number two: 99 (Number 10) Number three: 98 (Number 15) Number four: 97 (Number 11) Number five: 98 (Number 12)
There are many situations, I am just one of them , The other method is also 490
answer: 98+99+98+97+98=490

The second question is different substrings

A non-empty substring of a character string refers to a string consisting of a continuous segment of characters with
a length of at least 1 . For example, the string aaab has non-empty substrings a, b, aa, ab, aaa, aab, aaab, a total of 7 substrings.
Note that when calculating, only count the number of strings that are essentially different.
Excuse me, how many different non-empty substrings are there in the string 0100110001010001?
Solution: The requirement of the title is equivalent to the number of subsets of the string, so it can be solved directly by violence. It can be traversed from a character in the string until the end of the string. Every time it is traversed, it is stored in In the set (the value in the set is not allowed to be repeated, so choose it), the length of the final set is the answer

import java.util.*;
public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str = sc.next();  //读入字符串
		TreeSet<String> s = new TreeSet<String>();  // 把遍历到的字符串存到set集合中
		for(int i = 0; i < str.length(); i++){     //  从字符串的起点开始遍历
			for(int j = i; j < str.length(); j++){    // 从当前位置遍历到字符串的末尾
				s.add(str.substring(i, j + 1));    //  把每一遍历到的内容存入set集合中
			}
		}
		System.out.println(s.size());

	}

}

Answer: 100

The third question

Given a sequence of 1, 1, 1, 3, 5, 9, 17, …, starting from item 4, each item is the sum of the first three items. Find
the last 4 digits of item 20190324.
Problem solution: violent solution, three fors are enough, but the pitfall of this problem is that the final result must be left over, otherwise no matter what type, it will definitely burst. Unfortunately, I didn’t see it clearly in the examination room, and it was wrong.

import java.util.*;
public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] arr = new int[20190324];
		arr[0] = 1;
		arr[1] = 1;
		arr[2] = 1;
		for(int i = 3; i < 20190324; i++){
			arr[i] = arr[i-1] + arr[i-2] + arr[i-3];
			arr[i] %= 10000;
		}
		System.out.println(arr[20190323]);

	}

}

Answer: 4659

Number decomposition of the fourth question

Decompose 2019 into the sum of three different positive integers, and require that each positive integer does not
contain the numbers 2 and 4. How many different decomposition methods are there?
Note that the order of exchanging 3 integers is regarded as the same method. For example, 1000+1001+18 and
1001+1000+18 are regarded as the same kind of
problem solution: violently solve each number by violently, but the point is that The values ​​of the three numbers are different . I hope that I can read the questions before doing them. . .

import java.util.*;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int len = 2019 / 2;
		int sum = 0;
		for(int i = 1; i <= len; i++){
			for(int j = i + 1; j <= 2019; j++){
				for(int k = j + 1; k <= 2019; k++){ 
					String s = String.valueOf(i);
					String s1 = String.valueOf(j);
					String s2 = String.valueOf(k);
					if(i + j + k == 2019 && !s.contains("2") && !s.contains("4")
							&& !s1.contains("2") && !s1.contains("4")
							&& !s2.contains("2") && !s2.contains("4")){
						System.out.print(i);
						sum++;
					}
				}
			}
		}
		
		System.out.println(sum);
		
	}

}

Answer:  40785

Labyrinth

The following figure shows a plan view of a maze, where the ones marked as 1 are obstacles, and the ones marked as 0 are
accessible places.
010000
000100
001001
110000
The entrance of the maze is the upper left corner, and the exit is the lower right corner. In the maze, you can only walk from one position to
one of its four directions, up, down, left and right.
For the maze above, starting from the entrance, you can pass through the maze in the order of DRRURRDDDR, a
total of 10 steps. Among them, D, U, L, and R mean go down, up, left, and right respectively.
For the following more complicated maze (30 rows and 50 columns), please find a way to pass through the maze,
which uses the least number of steps. Under the premise of the least number of steps , please find the one with the least lexicographical order as the answer.
Please note that D<L<R<U in lexicographic order. (If you copy the following text into a text file, be sure
the content will be copied to check whether there is consistent with the document in a file directory questions maze.txt,
the same content with the following text)
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
[answer] submitted
This is a fill-in-the-blank question, you only need to calculate the result and submit it. The result of this question is a
string containing the four letters D, U, L, and R. When submitting the answer, only fill in this string, and fill in the
extra content will not be scored

Problem solution:  maze problem, template problem, equivalent to finding the shortest path from the upper left corner to the lower right corner, using breadth first search (BFS), using depth will time out

import java.util.*;
public class Main {

	static class Node{
		int x;
		int y;
		Node(int x, int y){
			this.x = x;
			this.y = y;
		}
	}
	static char[][] arr = new char[30][50];   //存放某个坐标的值
	static int[][] book = new int[30][50];  //标记这个坐标是否走过
	static int[][] result = new int[30][50];   //存入到达终点的路径长度
	static String[][] rs = new String[30][50];  //存入到达终点的方向
	public static void bfs(int x, int y) {
		int[][] next = {    //  枚举四个方向
				{0, 1},  // 右
				{1, 0},  //下
				{0, -1},  //左
				{-1, 0}   //上
		};
		Queue<Node> queue = new LinkedList<>();
		book[x][y] = 1;
		rs[0][0] = "";
		queue.add(new Node(x, y));
		while(!queue.isEmpty()) {
			Node node = queue.remove();
			for(int i = 0; i < 4; i++) {  //枚举四个方向
				int tx = node.x + next[i][0];
				int ty = node.y + next[i][1];
				if(tx < 0 || tx >= 30 || ty < 0 || ty >= 50) {//判断是否越界
					continue;
				}
				if(arr[tx][ty] == '0' && book[tx][ty] == 0) {   //符合条件执行
					book[tx][ty] = 1;
					result[tx][ty] = result[node.x][node.y] + 1; 
					rs[tx][ty] = rs[node.x][node.y] + i;
					queue.add(new Node(tx, ty));
				}
			}
		}
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		for(int i = 0; i < 30; i++) {    //存入某个坐标所代表的值
			String str = sc.next();
			for(int j = 0; j < 50; j++) {
				arr[i][j] = str.charAt(j);	
			}
		}
		bfs(0, 0);
		String st = rs[29][49];  
		for(int i = 0; i < st.length(); i++) {
			int c = st.charAt(i);
			if(c == '0') {
				System.out.print('R');
			}else if(c == '1'){
				System.out.print('D');
			}else if(c == '2') {
				System.out.print('L');
			}else if(c == '3') {
				System.out.print('U');
			}
		}
		System.out.println();
		System.out.println(result[29][49]);
		

	}

}

Answer:
Shortest path: 186 DDDDRRURRRRRRRRRDRRRDDDLDDRDDDDDDDDDDDDRDRDRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUUUULULLUUUUURRRRUULLLUUUULLUUULUURRRRRDRRRRRDDDLDDRDDDDDDDDDDRDRDRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUUULULLUUUUURRRRUULLLUUUULLUUULUURRURRRRURURRRDRDRRRRDRRLDRRDDDLDLLLRRRDDRDDLDDDDRDLLDDLLRRLDDDDL

The sixth question is the sum of special numbers

Xiao Ming is very interested in the numbers containing 2, 0, 1, 9 in the digits (excluding leading 0).
Such numbers from 1 to 40 include 1, 2, 9, 10 to 32, 39 and 40, a total of 28 , Their sum is 574.
Excuse me, what is the sum of all such numbers from 1 to n?

Solution: Convert an integer to a string and use a certain feature in the string (contains("")-indicates whether the string includes a certain character, if it exists, it returns true, otherwise it returns false).

import java.util.*;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int sum = 0;
		for(int i = 1; i <= n; i++) {
			String str = String.valueOf(i);
			if(str.contains("2") || str.contains("0") || str.contains("1") || str.contains("9")) {
				sum += i;
			}
		}
		System.out.println(sum);
		

	}

}

Question 7 Takeaway Priority

There are N take-out shops maintained in the "Are you full" takeaway system, numbered 1 N. Each takeaway store has
a priority, and the priority is 0 at the beginning (at time 0).
After each time unit, if the takeaway store has no order, the priority will be reduced by 1, and the lowest will be reduced
to 0; if the takeaway store has an order, the priority will not decrease but increase, and the priority will increase by 2 for each order.
If the priority of a takeaway restaurant is greater than 5 at a certain time, it will be added to the priority cache by the system; if the
priority is less than or equal to 3, it will be cleared from the priority cache.
Given the M pieces of order information within time T, please calculate how many takeaway shops are in the priority
cache at time T.
[Input format] The
first line contains 3 integers N, M and T.
Each of the following M lines contains two integers ts and id, indicating that the takeaway store with id at time ts received
an order.
[Output format]
Output an integer to represent the answer.
[Sample input]
2 6 6
1 1
5 2
3 1
6 2
2 1
6 2
[Sample output]
1
[Sample explanation]
At time 6, the priority of No. 1 shop drops to 3 and is removed from the priority cache ; The priority of No. 2 shop is raised to 6, and
priority cache is added. So there is 1 store (No. 2) in the priority cache.
answer: Simulation questions can be written step by step according to the topic. First, use a class to save all the information of a certain store (whether it is in the priority cache, the priority size, and the order at a certain time), and then traverse the takeaway store at time 1 to T The takeaway order information is used to determine whether the takeaway store exists in the priority cache. The pitfall of this question is that if a store has previously been stored in the priority cache, but as long as its priority cache does not fall below 3, It will still be in the priority cache.

import java.util.*;
public class Main {

	static class Node{
		int num;  //第几家店
		int status; //是否在优先缓存中  1--在    0--不在
		int st;  //优先级大小
		List<Integer> list = new ArrayList<>();  //  某时刻该外卖店有订单
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt(); 
		int t = sc.nextInt();
		int sum = 0;
		Node[] node = new Node[n+1];
		for(int i = 0; i < m; i++) {
			int tx = sc.nextInt();
			int ty = sc.nextInt();
			if(node[ty] == null) {
				node[ty] = new Node();
				node[ty].num = ty;
				node[ty].status = 0;
				node[ty].st = 0;
				node[ty].list.add(tx);
			}else {
				node[ty].list.add(tx);
			}
		}
		for(int j = 1; j <= n; j++) {   //遍历外卖店
			Collections.sort(node[j].list);  //把外卖店中的订单按时间排序(升序)
			for(int i = 1; i <= t; i++) { // 某一时刻
				if(node[j].list.size() > 0) {   //判断这家店有没有订单
					int ss = 0;
					if(node[j].list.get(0) == i) {   //判断这家店在某时刻是否有订单
						ss++;
						node[j].list.remove(0);
						while(node[j].list.size() > 0 && node[j].list.get(0) == i) {  //某时刻可能存在多个订单,所以循环遍历
							ss++;
							node[j].list.remove(0);
						}
						node[j].st += (ss * 2);
						if(node[j].st > 5) {
							node[j].status = 1;
						}
						
					}else {
						//判断优先级是否为0
						if(node[j].st == 0) {
							node[j].st = 0;
						}else {
							node[j].st -= 1;
							if(node[j].status == 1 && node[j].st <= 3) {
								node[j].status = 0;
							}
						}
					}
				}else {
					if(node[j].st > 0) {
						node[j].st -= 1;
						if(node[j].status == 1) {
							if(node[j].st <= 3) {
								node[j].status = 0;
							}
							
						}
					}
				}
			}
			if(node[j].status == 1) {
				sum++;
			}
		}
		
		System.out.println(sum);
		

	}

}

Time to update the following questions. . .

Guess you like

Origin blog.csdn.net/qq_41066066/article/details/88902355