On July 5, 2020, the test questions and detailed explanation of the first provincial competition of the 11th Blue Bridge Cup (group B of Java undergraduate course)

Posted by omarh2005 on Thu, 10 Feb 2022 12:07:00 +0100

  1. Results fill in the blanks (full score 5 points)
  2. Results fill in the blanks (full score 5 points)
  3. Results fill in the blanks (full score 10 points)
  4. Results fill in the blanks (full score 10 points)
  5. Results fill in the blanks (Full Score: 15 points)
  6. Programming (Full Score: 15 points)
  7. Programming (Full Score: 20)
  8. Programming (Full Score: 20)
  9. Program design (Full Score 25)
  10. Programming (Full Score: 25)

Question 1: decryption

Xiao Ming designed a method of article encryption: for each letter c, turn it into another character Tc. The following table shows the rules of character transformation:

For example, encrypt the string YeRi to obtain the string EaFn.

Xiao Ming has a random string, encrypted as

EaFnjISplhFviDhwFbEjRjfIBBkRyY

(it consists of 30 upper and lower case English letters, excluding line breaks). What is the original string?

(if you copy the above strings and tables into a text file, be sure to check whether the copied contents are consistent with those in the document. There is a file str.txt under the test question directory. The first line is the string above, and the next 52 lines are the contents of the table in turn.)

[answer submission]

This is a result filling question. You just need to calculate the result and submit it. The result of this question is a string containing only 30 uppercase and lowercase English letters. When submitting the answer, only fill in this string. If you fill in the extra content, you will not be able to score

public class Main {
	public static void main(String[] args) { // 30 characters
//		String res = "YeRik GSunl RzgDl vRwYk XkrGW WhXaA"; //  Unencrypted [answer]
//		String str = "EaFnj ISplh FviDh wFbEj RjfIB BkRyY"; //  After encryption
		String str = "EaFnjISplhFviDhwFbEjRjfIBBkRyY"; // Encrypted characters
		char[] arr = str.toCharArray();
		String s1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; // Original character
		String s2 = "yxmdacikntjhqlgoufszpwbrevYXMDACIKNTJHQLGOUFSZPWBREV"; // Encrypted characters
		String s3 = ""; // Decrypted characters
		for (int i = 0; i < arr.length; i++) {
			int j = s2.indexOf(arr[i]);   // Output the position of the current character in the encrypted character
			s3 += s1.substring(j, j + 1); // Find the character corresponding to the original character at this position
		}
		System.out.println(s3);
	}
}

Question 2: Anniversary

July 1, 2020 is the 99th anniversary of the founding of the Communist Party of China.

The Communist Party of China was founded on July 23, 1921.

How many minutes will it take from 12 noon on July 23, 1921 to 12 noon on July 1, 2020?

[answer submission]

This is a result filling question. You just need to calculate the result and submit it. The result of this question is an integer. When submitting the answer, only fill in this integer. If you fill in the redundant content, you will not be able to score.
 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class Main {
	// There is no need to look for 12 o'clock, because it is the same from 0 o'clock
	public static void main(String[] args) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date1 = format.parse("1921-7-23");
		Date date2 = format.parse("2020-7-1");
		int a = (int) ((date2.getTime() - date1.getTime()) / (1000 * 60));
		System.out.println(a);
	}
}

Question 3: merge detection

COVID-19, caused by COVID-19, has been spreading in A recently. In order to control the epidemic as soon as possible, A is ready to give large numbers of people to detect the nucleic acid of the virus.

However, kits for detection are in short supply.

In order to solve this difficulty, scientists have come up with a way: combined detection. The samples collected from multiple people (k) will be put into the same kit for testing. If the result is negative, it means that all k people are negative. The test of K people has been completed with a kit. If the result is positive, it means that at least one person is positive, and all samples of these K people need to be tested independently again (theoretically, if all K − 1 people are negative before the test, it can be inferred that the k-th person is positive, but this inference will not be used in practice, but K people will be tested independently), plus the initial combined test, A total of k + 1 kits were used to complete the detection of K individuals.

Country A estimates that the infection rate of the tested people is about 1%, which is evenly distributed. How many kits can k take to save money?

[answer submission]

This is a result filling question. You just need to calculate the result and submit it. The result of this question is an integer. When submitting the answer, only fill in this integer. If you fill in the redundant content, you will not be able to score.
 

public class Main {
	public static void main(String[] args) {
		int min = 999990;
		int ans = -1;
		for (int i = 1; i <= 100; i++) { // i test with one person
			int temp;
			if (100 % i != 0) {
				temp = 100 / i + i + 1;
			} else {
				temp = 100 / i + i;
			}
			if (min > temp) {
				min = temp;
				ans = i;
			}
		}
		System.out.println(ans);
	}
}

Question 4: distribution of masks

The mayor of a city has obtained several batches of masks. The number of masks in each batch is as follows: (if you copy the following text into a text file, be sure to check whether the copied content is consistent with that in the document. There is a file mask.txt in the test question directory, and the content is the same as the following text)

9090400

8499400

5926800

8547000

4958200

4422600

5751200

4175600

6309600

5865200

6604400

4635000

10663400

8087200

4554000

Now the mayor will distribute masks to two hospitals in the city. Due to the limitation of logistics, each batch of masks can only be allocated to one hospital. The mayor hopes that the smaller the difference between the total number of masks obtained by the two hospitals, the better. Please calculate the minimum difference?

[answer submission]

This is a result filling question. You just need to calculate the result and submit it. The result of this question is an integer. When submitting the answer, only fill in this integer. If you fill in the redundant content, you will not be able to score.
 

import java.util.Scanner;
 
public class Main { // 2400
	static int sum = 0;
	static int[] dp = new int[98090000];
	static int[] nums = { 0, 9090400, 8499400, 5926800, 8547000, 4958200, 4422600,
			5751200, 4175600, 6309600, 5865200, 6604400, 4635000, 10663400, 8087200, 4554000 };
 
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		for (int i = 1; i < nums.length; i++)
			sum += nums[i];
		int v = sum / 2;
		for (int i = 1; i < nums.length; i++) {
			for (int j = v; j >= nums[i]; j--) {
				dp[j] = Math.max(dp[j], dp[j - nums[i]] + nums[i]);
			}
		}
		System.out.println(dp[v]);
		System.out.println(sum - 2 * dp[v]);
	}
}

Question 5: maximum common divisor of Fibonacci sequence

Fibonacci sequence satisfies F1 = F2 = 1, and Fn = Fn − 1 + Fn − 2 from F3. Please calculate GCD(F2020, F520), where GCD(A, B) represents the maximum common divisor of A and B.

 

[answer submission]

This is a result filling question. You just need to calculate the result and submit it. The result of this question is an integer. When submitting the answer, only fill in this integer. If you fill in the redundant content, you will not be able to score.
 

import java.math.BigInteger;
 
public class Main { // 6765
	public static void main(String[] args) {
		BigInteger arr[] = new BigInteger[2025]; // int long data will explode array
		arr[0] = BigInteger.ZERO;
		arr[1] = arr[2] = BigInteger.ONE;
		for (int i = 3; i <= 2020; i++) {
			arr[i] = arr[i - 1].add(arr[i - 2]);
		}
		for (int i = 1; i <= 7; i++) { // Output element value verification of Fibonacci array
			System.out.println(i + ": " + arr[i]);
		}
		System.out.println(arr[520]);
		System.out.println(arr[2020]);
		System.out.println(arr[2020].mod(arr[520]));
//		System.out.println(gcd(arr[2020], arr[520]));
		System.out.println(arr[2020].gcd(arr[520]));
	}
 
	public static BigInteger gcd(BigInteger a, BigInteger b) {
		return b.equals(BigInteger.ZERO) ? a : gcd(b, a.mod(b));
//		if (b.compareTo(BigInteger.ZERO) == 0) {
//			return a;
//		}
//		return gcd(b, a.mod(b));
	}
}

Question 6: classification and counting

[problem description]

Input a string, please output how many uppercase letters, lowercase letters and numbers this string contains.

[input format]

The input line contains a string.

[output format]

Output three lines, one integer per line, representing the number of uppercase letters, lowercase letters and numbers respectively.

[sample input]

1+a=Aab

[sample output]

1 3 1

[evaluation case scale and agreement]

For all evaluation cases, the string consists of visible characters with a length of no more than 100.
 

import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String s = in.nextLine();
		char[] a = s.toCharArray();
		int upper = 0, lower = 0, num = 0;
		for (int i = 0; i < a.length; i++) {
			if (Character.isUpperCase(a[i]))
				upper++;
			else if (Character.isLowerCase(a[i]))
				lower++;
			else if (Character.isDigit(a[i]))
				num++;
		}
		System.out.print(upper + " " + lower + " " + num);
	}
}

Question 7: sum eight times

[problem description]

Given a positive integer n, find + + ··· + mod 123456789. Where mod represents remainder.

[input format]

The first line of input contains an integer n.

[output format]

Output a line containing an integer representing the answer.

[sample input]

2

[sample output]

257

[sample input]

987654

[sample output]

43636805

[evaluation case scale and agreement]

For 20% of the evaluation cases, 1 ≤ n ≤ 20.

For 60% of the evaluation cases, 1 ≤ n ≤ 1000.

For all evaluation cases, 1 ≤ n ≤ 1000000.
 

import java.math.BigInteger;
import java.util.Scanner;
 
public class Main {
	public static final BigInteger mod = BigInteger.valueOf(123456789);
 
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(); // For all evaluation cases, 1 ≤ n ≤ 1000000.
		BigInteger sum = BigInteger.ZERO; // sum will explode int\long
		for (int i = 1; i <= n; i++) {
			BigInteger temp = BigInteger.valueOf(i);
			sum = sum.add(temp.multiply(temp).multiply(temp).multiply(temp).
					multiply(temp).multiply(temp).multiply(temp).multiply(temp));
            // sum = sum.mod(mod);
		}
		System.out.println(sum.mod(mod));
	}
}
/**
int temp = (int) Math.pow(i, 8);
String str = temp + "";
long sum = 0;
for (int i = 1; i <= n; i++) {
	sum += (long)Math.pow(i, 8);
	sum %= mod;
	System.out.println(i + ": " + sum);
}
*/

Question 8: string encoding

[problem description]

Xiao Ming invented A method to encode A string composed of all uppercase letters. For each capital letter, Xiao Ming converts it into its serial number in 26 English letters, that is, A → 1, B → 2 Z → 26.

In this way, a string can be converted into a sequence of numbers:

For example, ABCXYZ → 123242526.

Now, given a converted number sequence, Xiao Ming wants to restore the original string. Of course, such a restore may have multiple strings that meet the conditions. Xiao Ming hopes to find the string with the largest dictionary order.

[input format]

A sequence of numbers.

[output format]

A string containing only uppercase letters, representing the answer.

[sample input]

123242526

[sample output]

LCXYZ

[evaluation case scale and agreement]

For 20% of the evaluation cases, the input length shall not exceed 20.

For all evaluation cases, the input length shall not exceed 200000.
 

import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		String s = input.next();
		int len = s.length();
		char[] c = s.toCharArray();
		int i;
		for (i = 0; i < len - 1; i++) {
			int a = c[i] - '0';
			int b = c[i + 1] - '0';
			int ans = a * 10 + b;
			if (ans < 27) {
				char ch = (char) (ans + 64);
				System.out.print(ch);
				i++;
			} else {
				char ch = (char) (a + 64);
				System.out.print(ch);
			}
		}
		if (i < len) {
			char ch = (char) (c[i] - '0' + 64);
			System.out.print(ch);
		}
	}
}

Question 9: BST insertion node problem

[problem description]

Given a binary tree with N nodes, the node number is 1 ∼ N. Among them, node i has weights, and the weights of these nodes just form a sorted binary tree (BST).

Now, given a node number K, Xiao Ming wants to know how many integers X (i.e. X is not equal to any) satisfy the N weights: add a child node with weight X to the node number K, and you can still get a BST.

For example, in the following figure, the numbers outside the brackets represent the number and the numbers inside the brackets represent the weight. That is, the node weights numbered 1 ∼ 4 are 0, 10, 20 and 30 in turn.

If K = 1, the answer is 0. Because node 1 already has left and right child nodes, no more child nodes can be added.

If K = 2, the answer is infinite. Because any negative number can be the left child of 2.

If K = 3, the answer is 9. Because X = 11, 12, ····, 19 can be used as the left child node of 3.

[input format]

The first line contains two integers N and K.

Each of the following N lines contains 2 integers, of which the ith line is the parent node number Pi and weight of the node numbered i. Note that Pi = 0 means i is the root node.

The input guarantee is a BST.

[output format]

An integer represents the answer. If the answer is infinite, output − 1.

[sample input]

4 3

0 10

1 0

1 20

3 30

[sample output]

9

[evaluation case scale and agreement]

For 60% of the evaluation cases, 1 ≤ K ≤ N ≤ 100, 0 ≤≤ 200, and ≤ are different.

For all evaluation cases, 1 ≤ K ≤ N ≤ 10000, 0 ≤≤ 100000000, and ≤ are different
 

import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		int k = input.nextInt();
		int a[] = new int[100100];
		int b[] = new int[100100];
		int vis[] = new int[100010];
		for (int i = 1; i <= n; i++) {
			a[i] = input.nextInt();
			b[i] = input.nextInt();
			vis[i] = a[i];
		}
		int cnt = 0;
		int ans = 0;
		for (int i = 1; i <= n; i++) {
			if (vis[i] == k) {
				cnt++;
				ans = b[i];
			}
		}
		if (cnt == 2)
			System.out.print(0);
		else if (cnt == 1) {
			ans = Math.abs(ans - b[k]);
			System.out.print(ans - 1);
		} else
			System.out.print(-1);
	}
}

Question 10: network analysis

[problem description]

Xiao Ming is doing a network experiment.

He set up n computers, called nodes, for sending, receiving and storing data.

Initially, all nodes are independent and there are no connections.

Xiao Ming can connect the two nodes through the network cable. After connecting, the two nodes can communicate with each other. If two nodes have network cable connection, they are called adjacent nodes.

Xiao Ming sometimes tests the network at that time. He will send a message at a node, and the message will be sent to each adjacent node. Then these nodes will forward it to their adjacent nodes until all directly or indirectly adjacent nodes receive the message. All sending and receiving nodes will store the information. A message is stored only once.

The process of Xiaoming connection and test is given. Please calculate the size of information stored in each node.

[input format]

The first line of input contains two integers n and m, which respectively represent the number of nodes and the number of operations. Nodes are numbered from 1 to n.

The next m lines, each with three integers, represent an operation.

If the operation is 1 a b, it means that node A and node b are connected through network cable. When a = b, it indicates that a self ring is connected and has no material impact on the network.

If the operation is 2 p t, it means that a message of size t is sent on node p.

[output format]

The output line contains n integers, and the adjacent integers are divided by a space, which successively represents the size of the information stored on node 1 to node n after the above operations.

[sample input]

4 8

1 1 2

2 1 10

2 3 5

1 4 1

2 2 2

1 1 2

1 2 4

2 2 1

[sample output]

13 13 5 3

[evaluation case scale and agreement]

For 30% of the evaluation cases, 1 ≤ n ≤ 20, 1 ≤ m ≤ 100.

For 50% of the evaluation cases, 1 ≤ n ≤ 100, 1 ≤ m ≤ 1000.

For 70% of the evaluation cases, 1 ≤ n ≤ 1000, 1 ≤ m ≤ 10000.

For all evaluation cases, 1 ≤ n ≤ 10000, 1 ≤ m ≤ 100000, 1 ≤ t ≤ 100
 

import java.util.LinkedList;
import java.util.Scanner;
 
public class Main {
	static int[] data;
	static boolean[] bool;
	static LinkedList<LinkedList<Integer>> list = new LinkedList<LinkedList<Integer>>();
 
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		data = new int[n + 1];
		bool = new boolean[n + 1];
		int a = 0, b = 0, c = 0;
		for (int i = 0; i <= n; i++) {
			list.add(new LinkedList<>());
		}
		for (int i = 0; i < m; i++) {
			a = sc.nextInt();
			b = sc.nextInt();
			c = sc.nextInt();
			// Connect them to each other
			if (a == 1) {
				list.get(b).add(c);
				list.get(c).add(b);
			} else {
				bool = new boolean[n + 1];
				dfs(b, c);
			}
		}
		for (int i = 1; i <= n; i++) {
			System.out.print(data[i] + " ");
		}
	}
 
	// dfs traverses every node
	public static void dfs(int node, int num) {
		bool[node] = true;
		data[node] += num;
		LinkedList<Integer> templist = list.get(node);
		for (int i : templist) {
			if (!bool[i]) {
				dfs(i, num);
			}
		}
	}
}

Topics: Java