Test questions and solutions of the 13th Blue Bridge Cup (phase III) C++

Posted by drdokter on Fri, 28 Jan 2022 14:25:09 +0100

Test questions and solutions of the 13th Blue Bridge Cup Simulation Competition (phase III)

1. Question A

[problem description]

What is the decimal system corresponding to the hexadecimal number 2021ABCD?

Number system conversion

A ~ F in hexadecimal indicates 10 ~ 15. Finally, use% x placeholder to output hexadecimal for checking

//Answer 539077581
#include <iostream>
using namespace std;

int main() {
	string s = "2021ABCD";
	int bac = 1, ans = 0, tmp;
	for (int i = s.length() - 1; i >= 0; i--) {
		if (s[i] >= '0' && s[i] <= '9') {
			tmp = s[i] - '0';
		}
		else tmp = 10 + s[i] - 'A';
		ans += tmp * bac;
		bac *= 16;
	}
	cout << ans << endl;
	printf("%x\n", ans);	//Output hex check
	return 0;
}
//Answer 539077581
#include <iostream>
#include <cmath>
using namespace std;

int main() {
	int ans = 0;
	ans +=  13;
	ans +=  12 * 16;
	ans +=  11 * pow(16, 2);
	ans +=  10 * pow(16, 3);
	ans +=  1 * pow(16, 4);
	ans +=  2 * pow(16, 5);
	ans +=  2 * pow(16, 7);
	cout << ans << endl;
	printf("%x\n", ans);
	return 0;
}

2. Question B

[problem description]

If an integer m is A multiple of integers A and B at the same time, M is said to be the common multiple of A and B, and the smallest positive integer in the common multiple is called the least common multiple.

For example, the least common multiple of 2021 and 86 is 4042.

From 1 (inclusive) to 2021 (inclusive), how many numbers and the least common multiple of 2021 is 4042.

Solution: Enumeration

//Answer 89
#include <iostream>
using namespace std;

int gcd(int a, int b) {
	return (a % b == 0) ? b : gcd(b, a % b);
} 
int lcm(int a, int b) {
	return a / gcd(a, b) * b;
}

int main() {
	int ans = 0;
	for (int i = 1; i <= 2021; i++) {
		if (lcm(i, 2021) == 4042) {
			ans++;
		}
	}
	cout << ans << endl;
	return 0;
}

3. Test question C

[problem description]

10 is a very special number, which can be expressed as the sum of the squares of two non negative integers, 10 = 3 * 3 + 1 * 1.

9 is also a special number, which can be expressed as 9 = 3 * 3 + 0 * 0.

How many such numbers are there from 1 to 2021?

Please note that some numbers can be expressed in many ways, for example, 25 = 5 * 5 + 0 * 0 = 3 * 3 + 4 * 4, which is calculated only once when calculating the answer.

Solution: Enumeration

//Answer 624
#include <iostream>
using namespace std;

int main() {
	int a[2022] = {0};
	int ans = 0;
	for (int i = 0; i <= 200; i++) {
		for (int j = 0; j <= i; j++) {
			int tmp = i * i + j * j;
			if (tmp <= 2021) a[tmp] = 1;
		}
	}
	for (int i = 1; i <= 2021; i++) {
		ans += a[i];
	}
	cout << ans << endl;
	return 0;
}

4. Question D

[problem description]

The following is the adjacency matrix representation of an undirected graph with 8 nodes, in which row I and column J represent the edge length from node i to node j. When the length is 0, there is no edge.

0 9 3 0 0 0 0 9
9 0 8 1 4 0 0 0
3 8 0 9 0 0 0 0
0 1 9 0 3 0 0 5
0 4 0 3 0 7 0 6
0 0 0 0 7 0 5 2
0 0 0 0 0 5 0 4
9 0 0 5 6 2 4 0

What is the minimum spanning tree size of this graph?

Minimum spanning tree

Arrange all the side lengths from small to large, and take the smallest side to connect the two nodes each time. If the two nodes are already connected, they will not be connected

Loop through the above steps to know that all nodes are connected, and the sum of all edges is the answer

The connection results are as follows: (the node number starts from 0, and the drawing on the paper is clearer)

1 → 3 (side length 1), 5 → 7 (side length 2), 0 → 2 (side length 3), 3 → 4 (side length 3), 6 → 7 (side length 4), 3 → 7 (side length 5), 1 → 2 (side length 8),

The answer is: 1 + 2 + 3 + 3 + 4 + 5 + 8 = 26

The procedure to get the answer is as follows: (use the union search set to find whether the two nodes are connected)

//Take the adjacency matrix as the input to get the answer
#include <bits/stdc++.h>
using namespace std;
const int n = 8;
int f[n], ans = 0;
vector<pair<int, int> > a;

int findf(int idx) {
	if (f[idx] == idx) return idx;
	f[idx] = findf(f[idx]);
	return f[idx];
}

int main() {
	for (int i = 0; i < n; i++) {
		f[i] = i;
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int len;
			cin >> len;
			if (len) {	//Here i and j are compressed into one number
				a.push_back(make_pair(len, i * 100 + j));
			}
		}
	}
	sort(a.begin(), a.end());
	for (int i = 0; i < a.size(); i++) {
		int len = a[i].first;
		int x = a[i].second / 100;
		int y = a[i].second % 100;
		if (findf(x) != findf(y)) {
			f[f[x]] = f[y];
			ans += len;
			printf("%d --> %d (%d)\n", x, y, len);
		}
	}
	cout << ans << endl;
	return 0;
}

5. Test question E

[problem description]

The following is a 20 * 20 matrix. Each number in the matrix is a number between 1 and 9. Please note that the separator is removed during display.

69859241839387868941
17615876963131759284
37347348326627483485
53671256556167864743
16121686927432329479
13547413349962773447
27979945929848824687
53776983346838791379
56493421365365717745
21924379293872611382
93919353216243561277
54296144763969257788
96233972513794732933
81443494533129939975
61171882988877593499
61216868895721348522
55485345959294726896
32124963318242554922
13593647191934272696
56436895944919899246

The value of a sub matrix in a matrix refers to the sum of all values in the sub matrix.

What is the value of a 5 * 5 submatrix with the largest value in the matrix?

Method 1: violent summation

The data range is not large enough to be violent

//Take the matrix in the question as input to get the answer
//Answer 154
#include <bits/stdc++.h>
using namespace std;
const int n = 20;
int a[n][n], ans = 0;
int get_sum(int x, int y) {	//Summation complexity O(n^2) 
	int sum = 0;
	for (int i = x - 4; i <= x; i++) {
		for (int j = y - 4; j <= y; j++) {
			sum += a[i][j];
		}
	}
	return sum;
}
int main() {
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		for (int j = 0; j < n; j++) {
			a[i][j] = s[j] - '0';
		}
	}
	for (int i = 4; i < n; i++) {
		for (int j = 4; j < n; j++) {
			ans = max(ans, get_sum(i, j));
		}
	}
	cout << ans << endl;
	return 0;
}

Method 2: one dimensional prefix and

//Ordinate offset 1
//Answer 154
#include <bits/stdc++.h>
using namespace std;
const int n = 20;
int a[n][n + 1], ans = 0;

int get_sum(int x, int y) {	//Summation complexity O(n) 
	int sum = 0;
	for (int i = x - 4; i <= x; i++) {
		sum += a[i][y] - a[i][y - 5];
	}
	return sum;
}

int main() {
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		for (int j = 0; j < n; j++) {
			a[i][j + 1] = s[j] - '0';
			a[i][j + 1] += a[i][j];
		}
	}
	for (int i = 4; i < n; i++) {
		for (int j = 5; j <= n; j++) {
			ans = max(ans, get_sum(i, j));
		}
	}
	cout << ans << endl;
	return 0;
}

Method 2: two-dimensional prefix and

//Both horizontal and vertical coordinates are offset by 1
//Answer 154
#include <bits/stdc++.h>
using namespace std;
const int n = 20;
int a[n + 1][n + 1], ans = 0;

int get_sum(int x, int y) {	//Summation complexity O(1) 
	return a[x][y] - a[x - 5][y] - a[x][y - 5] + a[x - 5][y - 5];
}

int main() {
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		for (int j = 0; j < n; j++) {
			a[i + 1][j + 1] = s[j] - '0';
			a[i + 1][j + 1] += a[i + 1][j] + a[i][j + 1] - a[i][j];
		}
	}
	for (int i = 5; i < n; i++) {
		for (int j = 5; j <= n; j++) {
			ans = max(ans, get_sum(i, j));
		}
	}
	cout << ans << endl;
	return 0;
}

6. Test question F

[problem description]

Xiaolan wants to write a web page to display some goods.

There are t commodities in total. They are numbered from 1 to t in order. a is displayed on each page. What is the minimum and maximum number displayed on page p?

[input format]

The input line contains three integers t, a and p, and the adjacent integers are separated by a space.

[output format]

The output line contains two integers, representing the minimum and maximum numbers respectively.

[sample input]

31 10 3

[sample output]

21 30

[sample input]

31 10 4

[sample output]

31 31

[evaluation case scale and agreement]

For all evaluation cases, 1 < = T < = 1000, 1 < = a < = 100, 1 < = p. Ensure that at least one item is displayed on page p

Problem solution

The minimum number is a * (p - 1) + 1, and the maximum number is min(t, a * p)

#include <iostream>
using namespace std;

int main() {
	int t, a, p;
	cin >> t >> a >> p; 
	cout << a * (p - 1) + 1 << " " << min(t, a * p) << endl;
	return 0;
}

7. Test question G

[problem description]

Given a positive integer n, please judge whether the values in all digits of N are strictly increasing from left to right.

For example, 1589 is strictly increasing.

Another example: 1336 is not strictly increasing, and there is the same 3 in the middle.

Another example: 1598 is not strictly increasing.

[input format]

The input line contains a positive integer n.

[output format]

If it is strictly incremental, output "YES" (all uppercase), otherwise output "NO" (all uppercase).

[sample input]

1589

[sample output]

YES

[sample input]

1336

[sample output]

NO

[evaluation case scale and agreement]

For all evaluation cases, 1 < = n < = 1000000000.

Solution: Digital split

#include <iostream>
using namespace std;

bool check(int x) {
	int last = 10;
	while (x) {
		int tmp = x % 10;
		if (tmp >= last) return false;
		last = tmp;
		x /= 10;
	}
	return true;
}
int main() {
	int n, last = 10;
	cin >> n;
	if (check(n)) cout << "YES" << endl;
	else cout << "NO" << endl;	
	return 0;
}

8. Test question H

[problem description]

Xiao Lan parked her car on the roadside and drove away on the same day. Given the parking time and driving time, how long did Xiaolan stop?

[input format]

Enter two lines. The first line contains parking time and the second line contains driving time.

The format of each time is HH:MM:SS, where HH indicates that when the value is an integer from 0 to 23, if it is less than 10, fill in two digits with 0; MM and SS represent minutes and seconds respectively. The value is an integer from 0 to 59. When it is less than 10, 0 is used to fill in two digits.

[output format]

Output the total parking time in HH:MM:SS format.

[sample input]

08:58:10
17:20:31

[sample output]

08:22:21

Problem solution

It is more convenient to control the output with placeholders in C language

#include <cstdio>

int main() {
	int begin, end, ans;
	int hh, mm, ss;
	scanf("%d:%d:%d", &hh, &mm, &ss);
	begin = hh * 3600 + mm * 60 + ss;
	scanf("%d:%d:%d", &hh, &mm, &ss);
	end = hh * 3600 + mm * 60 + ss;
	ans = end - begin;
	printf("%02d:%02d:%02d\n", ans / 3600, (ans / 60) % 60, ans % 60);
	return 0;
}

9. Test question I

[problem description]

n athletes participate in a sports meeting composed of m sports, and each athlete is required to participate in each event.

Each athlete has a result in each event. The greater the result, the higher the ranking. The results of different athletes will not be the same in each project, so the ranking will not be the same. (however, different projects may have the same results)

The top k of each project will get k to 1 points respectively, and the i-th will get max(k+1-i, 0) points.

The total score of each athlete is the sum of his scores in each event.

Please calculate the total score of each athlete.

[input format]

The first line of input contains two integers n, m, k, separated by a space.

The next n lines contain m integers, and the j integer in the i line represents the result of the i athlete in the j competition.

[output format]

The output line contains n integers, which represent the total score of each athlete in turn, and the adjacent integers are separated by a space.

[sample input]

3 5 2
5 3 1 5 12
2 4 2 34 1
8 6 3 2 2

[sample output]

4 4 7

[example description]

The score of the first athlete is: 1 + 0 + 0 + 1 + 2 = 4
The score of the second athlete is: 0 + 1 + 1 + 2 + 0 = 4
The score of the third athlete is: 2 + 2 + 2 + 0 + 1 = 7

[evaluation case scale and agreement]

For 50% of the evaluation cases, 2 < = n, m, K < = 20, 0 < = score < = 1000.
For all evaluation cases, 2 < = n, m, K < = 100, 0 < = score < = 10000.

Solution: shape pressure sorting

Since the number of athletes does not exceed 100, you can multiply the result by 107 and add the number of athletes to compress the state. Of course, you can also use pair

Place the matrix vertically, save the results and numbers of each athlete of a certain sport in each row, and then sort each row in descending order

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
int a[MAXN][MAXN];
int ans[MAXN];

bool cmp(int x, int y) {
	return x > y;
}

int main() {
	int n, m, k;
	cin >> n >> m >> k;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> a[j][i];
			a[j][i] = a[j][i] * 107 + i;
		}
	}
	for (int i = 0; i < m; i++) {
		sort(a[i], a[i] + n, cmp);
		for (int j = 0; j < k && j < n; j++) {
			int idx = a[i][j] % 107;
			ans[idx] += k - j;
		}
	}
	for (int i = 0; i < n; i++) {
		cout << ans[i];
        if (i != n - 1) cout << " ";
	}
	cout << endl;
	return 0;
}

10. Question J

[problem description]

Given n integers a[1], a[2],..., a[n], Xiaolan wants to select a part in the middle, which meets the following two conditions:

1. For a subscript set S, at least k subscripts in the selected number are in the set S;

2. The selected number is arranged in the original order, which is strictly monotonically rising, that is, the selected number is a rising subsequence.

How many numbers can Xiaolan choose at most.

[input format]

The first line of input contains two integers n, k, separated by a space.

The second line contains n integers a[1], a[2],..., a[n], and the adjacent integers are separated by spaces.

The third row contains a 01 string with a length of n, which indicates whether each subscript is in the set s in turn, 0 means not in s, and 1 means in S.

[output format]

The output line contains an integer representing the answer. If there is no selection method that meets the conditions, output - 1.

[sample input]

8 2
8 1 2 3 9 4 7 10
10001010

[sample output]

3

[example description]

Since at least two of the three numbers 8, 9 and 7 must be selected, only 8 and 9 can be selected, and the remaining number can only be the last number 10.

[sample input]

8 3
8 1 2 3 9 4 7 10
10001010

[sample output]

-1

[evaluation case scale and agreement]

For 30% of the evaluation cases, 2 < = n < = 100, 0 < = a [i] < = 100, 0 < = k < = 3.
For all evaluation cases, 2 < = n < = 1000, 0 < = a [i] < = 100000, 0 < = k < = 20.

Violent deep search

Not even 30% of the samples

I hope a big man can give me some advice

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;

int a[MAXN];
string s;
int n, k, ans = -1;
void dfs(int cnt, int len, int idx) {
	if (s[idx] == '1') cnt++;
	len++;
	if (idx == n - 1 && cnt >= k) {
		ans = max(ans, len);
	}
	for (int i = idx + 1; i < n; i++) {
		if (a[i] > a[idx]) {
			dfs(cnt, len, i);
		}
	}
}

int main() {
//	freopen("input.txt", "r", stdin);
	cin >> n >> k;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	cin >> s;
	for (int i = 0; i < n; i++) {
		dfs(0, 0, i);
	}
	cout << ans << endl;
	return 0;
}

Topics: C++ Algorithm