2019 10th Blue Bridge Cup C/C++ Provincial Competition Group B Real Questions + Answers

 The answers and codes are for reference only. It took too much time for the penultimate question during the competition, resulting in no time to write the last question of the fill-in-the-blank question and the last question of the big question. Alas, in general, this year's questions are simpler than last year's. This year The Blue Bridge Cup is really a "violent cup". Let's put so many things first, and then update them later, welcome to provide code, hahahahaha

Test question link: https://pan.baidu.com/s/1ZJ09aV-denor-MSGOnJpDA 
Extraction code: bqnz 
Copy this content and open the Baidu SkyDrive mobile app, the operation is more convenient

content

Question A: Team Up

Question B: Year String

Question C: Evaluation of Sequences

Question D: Decomposition of Numbers 

Question E: Maze 

Question F: Sum of Special Numbers 

Question G: Weights of Complete Binary Trees 

Question H: Arithmetic Sequence 

Question I: Postfix Expressions 

Question J: Psionic Transmission


 Question A: Team Up

Question A: Very simple question, not much to say, just search directly, code: answer 490

#include<bits/stdc++.h>
using namespace std;
int team[20][6];
int vis[20];
int max_sum = 0;
void dfs(int index, int sum){
	if(index == 6){
		max_sum = max(max_sum, sum);
		return;
	}
	for(int i = 0; i < 20; i++){
		if(!vis[i]){
			vis[i] = 1;
			dfs(index + 1, sum + team[i][index]);
			vis[i] = 0;
		}
	}
}
int main(){
	freopen("team.txt", "r", stdin); //读team.txt文件 ,输入重定向 
        //注意把team.txt放到同一级目录下,或者把这行注释掉,手动输入 
	for(int i = 0; i < 20; i++)
		for(int j = 0; j < 6; j++)
			cin>>team[i][j];
	dfs(1, 0);
	cout<<max_sum<<endl;
	return 0;
}

 data:

1 97 90 0 0 0
2 92 85 96 0 0
3 0 0 0 0 93
4 0 0 0 80 86
5 89 83 97 0 0
6 82 86 0 0 0
7 0 0 0 87 90
8 0 97 96 0 0
9 0 0 89 0 0
10 95 99 0 0 0
11 0 0 96 97 0
12 0 0 0 93 98
13 94 91  0 0 0
14 0 83 87 0 0
15 0 0 98 97 98
16 0 0 0 93 86
17 98 83 99 98 81
18 93 87 92 96 98
19 0 0 0 89 92
20 0 99 96 95 81

Question B: Year String

Question B: At first I thought it was 26 hexadecimal,,,,,, but it was not, code: Answer BYQ

#include<bits/stdc++.h>
using namespace std;
int main(){
	string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	char ans[5];
	int index = 0;
	int n = 2019;
	while(n){
		int t = n % 26;
		n = n / 26;
		ans[index] = str[t - 1];
		index++; 
	}
	for(int i = index - 1; i >= 0; i--){
		cout<<ans[i];
	}
	return 0;
}

Question C: Evaluation of Sequences

Question C: The question requires the last four digits, then we only need to modulo 10000 after each time f(n) is calculated, because the last 4 digits of the addition are only related to the last 4 digits of the addend, code: answer 4659

#include<bits/stdc++.h>
using namespace std;
int f[20190324];//数组空间太大,要放到main外面
int main(){
	f[0] = f[1] = f[2] = 1;
	for(int i = 3; i < 20190324; i++){
		f[i] = (f[i - 1] + f[i - 2] + f[i - 3]) % 10000;
	} 
	cout<<f[20190323]<<endl;
	return 0;
}

Question D: Decomposition of Numbers 

Question D: Very simple, two layers of for loops i and j, j starts from i+1 to ensure that ijk is in ascending order, k=2019-ij, it is enough to judge that the ijk package does not contain 2 and 4, code: answer 40785

#include<bits/stdc++.h>
using namespace std;
int check(int n){
	while(n){
		int t = n % 10;
		n = n / 10;
		if(t == 2 || t == 4)
			return 0;
	}
	return 1;
}
int main(){
	int ans = 0;
	for(int i = 1; i <2019; i++){
		if(check(i))
			for(int j = i + 1; j <2019; j++){
				if(check(j)){
					int k = 2019 - i - j;
					if(k > j && check(k))
						ans++;
				}
			}
	}
	cout<<ans<<endl;
	return 0;
}

Question E: Maze 

data:

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

Question E: This question is basically the same as the subject of Xueba's maze http://lx.lanqiao.cn/problem.page?gpid=T291 in the algorithm improvement part of the Lanqiao practice system, so there is nothing wrong with brushing the practice system. See the original title of the official website:

Answer (probably wrong, just for reference):

DDDDRRURRRRRRRDRRRDDDLDDRDDDDDDDDDDRDRDRDRRURUUURRRRDDDRDRRRRRRRRRRDRDDDRRRRRRURRURDRDRDRD.

Idea: first search widely, and search in the direction of DLRU. In this way, the first answer found is the smallest lexicographical order. Use a queue, first enter 0(x), 0(y) into the queue, and then judge according to the direction of DLRU. Is it possible to go. Use a vis[i][j] to store the depth of the current node, the starting point is 1, and the following is the result of vis[i][j]. In fact, the answer at this step can be seen by yourself, but for convenience, use Do a deep search to find the answer.

Code: 

#include<bits/stdc++.h>
using namespace std;
string maze[30]; //存放迷宫 
int vis[30][50]; //是否访问过 
char ans[1000]; //答案序列 
queue<int> que; 
int next_x[4] = {1, 0, 0, -1}; //注意x,y的顺序,D<L<R<U搜出来的必定字典序最小
int next_y[4] = {0, -1, 1, 0}; //{x, y} = {1, 0}表示向下 ,{0, -1}左,{0, 1}右,{-1, 0}上 
int DLRU[4] = {'D', 'L', 'R', 'U'};
int flag = 0; //用于dfs中 
void bfs(int x, int y){
	que.push(x);
	que.push(y);
	vis[x][y] = 1;
	while(!que.empty()){
		int current_x = que.front();
		que.pop();
		int current_y = que.front();
		que.pop();
		if(current_x == 29 && current_y == 49) //如果走到了终点就退出循环 
			break;
		for(int i = 0; i < 4; i++){ //这里是判断四个方向是否可到达 
			int next_step_x = current_x + next_x[i];
			int next_step_y = current_y + next_y[i];
			if(next_step_x < 30 && next_step_x >= 0 && next_step_y < 50 && next_step_y >= 0){ //不能越界 
				if(maze[next_step_x][next_step_y] == '0') //如果是0代表可达 
					if(vis[next_step_x][next_step_y] == 0){ //判断这个点有没有访问过,等于0代表没有访问过 
						que.push(next_step_x);
						que.push(next_step_y);
						vis[next_step_x][next_step_y] = vis[current_x][current_y] + 1; //当前节点vis值等于上一层+1,这样方便后面寻找走的路径 
					}
			}
		}
	}
}
void dfs(int index, int x, int y){//这里深搜是从终点开始,所以要按照字典序最小,那么方向顺序就和正向相反,就是URLD 
	if(flag == 1) return; //如果找到了第一个答案序列,不进行下面的搜索 
	if(index == 1){
		flag = 1; // 用来判断是否搜到第一个答案,如果搜到了接下来的搜索就不用干了 
		for(int i = 2; i <= vis[29][49]; i++)
			cout<<ans[i];
		cout<<endl;
		return;
	}
	for(int i = 3; i >= 0; i--){ //顺序要相反 
		int pre_x = x + next_x[i];
		int pre_y = y + next_y[i];
		if(pre_x < 30 && pre_x >= 0 && pre_y < 50 && pre_y >= 0){
			if(vis[pre_x][pre_y] + 1 == vis[x][y]){
				ans[index] = DLRU[3 - i]; 
				dfs(index - 1, pre_x, pre_y);
				ans[index] = ' ';
			}
		}
	}
}
int main(){
	freopen("maze.txt", "r", stdin); //注意把maze.txt放到同一级目录下,或者把这行注释掉,手动输入 
	for(int i = 0; i < 30; i++)
		cin>>maze[i];
	bfs(0, 0);
	dfs(vis[29][49], 29, 49);
	for(int i = 0; i < 30; i++){
		for(int j = 0; j < 50; j++){
//			cout<<vis[i][j]<<" ";
			printf("%3d ", vis[i][j]);
		}
		cout<<endl;
	}
	return 0;
}

Question F: Sum of Special Numbers 

 

Test question F (note that the description of the input format of the question is wrong, it is an integer): I think this question is a deformation of the fill-in-the-blank question D. Look at the test cases from 1 to 10000, search directly, the code:

#include<bits/stdc++.h>
using namespace std;
int check(int n){
	while(n){
		int t = n % 10;
		n = n / 10;
		if(t == 2 || t == 0 || t == 1 || t == 9)
			return 1;
	}
	return 0;
}
int main(){
	int n;
	int sum = 0;
	cin>>n;
	for(int i = 1; i <= n; i++){
		if(check(i)){
			sum += i;
		}
	}
	cout<<sum<<endl;
	return 0;
}

Question G: Weights of Complete Binary Trees 

Question G: Idea: processing while inputting, we can easily know that the subscript of the last number of each line is equal to (2^n)-1, for example, the last number of the second line is equal to 2^2-1=3 , the last number of the third line is equal to 2^3-1=7, use deep to represent the current depth, add from the first item of the current line to the last item and then compare it with the maximum value max_sum. Code:

#include<bits/stdc++.h>
using namespace std;
int Ai[100005];
int main(){
	int N;
	int deep = 1; //深度 
	int sum = 0; //每行的和 
	long long max_sum = -100000000000; //最大的和 
	int max_deep = 1;
	cin>>N;
	for(int i = 1; i <= N; ++i){
		cin>>Ai[i];
		sum += Ai[i];
		if(i == pow(2, deep) - 1){
			if(max_sum < sum){ //注意不要取等号,因为题目要最小的深度 
				max_deep = deep;
				max_sum = sum;
			}
			sum = 0;
			++deep;
		}
	}
	cout<<max_deep<<endl;
	return 0;
}

Question H: Arithmetic Sequence 

 

Question H: When I got this question, I thought of sorting first, then finding the smallest tolerance, and then ((maximum-minimum)/minimum tolerance)+1, but this is a wrong idea , alas, I lost points again . . . . . For example, if you enter 6 2 4 7 10 12 20, if you use this formula to calculate the answer, the answer is 10, but the actual value is 19, so it is to find the greatest common factor of each two adjacent numbers after sorting. Code:

#include<bits/stdc++.h>
using namespace std;
int Ai[100005];
int main(){
	int N;
	cin>>N;
	for(int i = 0; i < N; i++)
		cin>>Ai[i];
	sort(Ai, Ai + N);
	int min_d = Ai[1] - Ai[0]; //升序排的序,所以公差大于0 
	if(min_d == 0) //如果 Ai[1] - Ai[0]等于0,说明这些数必定全部相等,那最小值就是N 
		cout<<N<<endl;
	else{
		int gcd = min_d; 
		for(int i = 2; i < N; i++){
			min_d = __gcd(min_d, Ai[i] - Ai[i - 1]); 
		}
		cout<<(Ai[N - 1] - Ai[0]) / min_d + 1<<endl;
	}
	return 0;
}

Question I: Postfix Expressions 

 

Question I: This damn question, the exam wasted a lot of time on me. . .

Question J: Psionic Transmission

 

Question J: The exam didn't read the questions, you know. . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325347807&siteId=291194637