2019 第十届蓝桥杯C/C++ 省赛B组题解

   明年是我最后一次蓝桥杯比赛,是最后一次展现自己实力的时候,下面对第十届的蓝桥杯试题做一下总结,好好加油

这个是今年所有的试题链接先发一下:https://www.cnblogs.com/fisherss/p/10590410.html

试题A:组队

题目关键字:首发阵容一号位到五号位评分之和的最大值,针对每个号位选取分数最大的人,最后评分求和就可以

试题B:年号字串

#include <iostream>
using namespace std;

void solve(int n) {
	if (!n) {
		return ;
	}
	solve(n / 26);
	cout << (char)(n % 26 + 64);
}

int main() {
	solve(2019);
	return 0;
} 

这一个题目就是模拟一个十进制的数字转换为26进制,就是简单的整除和取模的运算

试题C:数列求值

#include <iostream>
using namespace std;

int solve(int n) {
	if (n <= 3) {
		return 1;
	}
	int a = 1, b = 1, c = 1, res;
	for (int i = 4; i <= n; i++) {
         // 这里要记得取余
		res = (a + b + c) % 10000;
		a = b;
		b = c;
		c = res;
	}
	return res;
}

int main() {
	cout << solve(20190324) << endl;
	return 0;
}

优化每一次所取到的数字,然后进行取模运算,使用递归的话会出现爆栈

试题 D: 数的分解

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

// 判断某个数字位中是否包含 2 和 4 
bool judge(int n) {
	int t;
	while (n) {
		if (((t = n % 10) == 2) || t == 4) {
			return true;
		}
		n /= 10;
	}
	return false;
}

bool check(int a, int b, int c) {
	// 有数字出现 2 和 4,或者出现重复数字,返回 0 
	if (judge(a) || judge(b) || judge(c) || 
		a == b || a == c || b == c) {
		return false;
	}
	return true;
}

/**
 * 第一种思路:直接枚举后将结果 / 6
 */
int main() {
	int res = 0;
	for (int i = 1; i < 2018; i++) {
		for (int j = 1; j < 2018; j++) {
			for (int k = 1; k < 2018; k++) {
				if (i + j + k == 2019) {
					res += check(i, j, k);
				}
			}
		}
	}
	cout << (res / 6) << endl;
	return 0;
} 

/**
 * 第二种思路:循环的时候控制变量的起始值:
 */
int main() {
	int res = 0;
	for (int i = 1; i < 2018; i++) {
		for (int j = i + 1; j < 2018; j++) {
			for (int k = j + 1; k < 2018; k++) {
				if (i + j + k == 2019) {
					res += check(i, j, k);
				}
			}
		}
	}
	cout << res << endl;
	return 0;
} 

简单的暴力,没啥意思

试题 E: 迷宫

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
————————————————
这个是迷宫的地图,需要找出一个出口来出去,一般使用dfs来进行模拟但是用dfs来很容易超时,爆栈,所以采用bfs来解,每一步得到的都是最优解

#include <iostream>
#include <cstdio>  
using namespace std;

int map[][50] = {
	0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 
    0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 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, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 
    1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0
};
const int n = 30, m = 50;
int res = 0x7fffffff;
int book[n][m];

struct node {
	int x;
	int y;
	int s; // 路程 
	int f; // 上一个位置下标 
	char ch; // 上一个走向 
};
node que[n*m];
int head, tail;
// 按字典序从小到大顺序排列
int next_[4][2] = {
    {1, 0}, //下
    {0, -1}, // 左
    {0, 1}, // 右
    {-1, 0} // 上
};

void print(int index) {
	if (index == 0) {
		return ;
	}
	print(que[index].f);
	cout << que[index].ch;
}

void bfs() {
	que[tail].x = 0;
	que[tail].y = 0;
	que[tail].s = 0;
	que[tail].f = -1;
	que[tail++].ch = 0;
	book[0][0] = 1;
	int flag = 0;
	
	while(head < tail) {
		int nX, nY;
		for(int i = 0; i < 4; i++) {
			nX = next_[i][0] + que[head].x;
			nY = next_[i][1] + que[head].y;
			if (nX < 0 || nX >= n || nY < 0 || nY >= m) {
				continue;
			}
			if (map[nX][nY] == 0 && book[nX][nY] == 0) {
				book[nX][nY] = 1;
				que[tail].x = nX;
				que[tail].y = nY;
				que[tail].s = que[head].s + 1;
				que[tail].f = head;
				if (next_[i][0]) {
					que[tail].ch = (next_[i][0] == 1 ? 'D' : 'U');
				} else if (next_[i][1]) {
					que[tail].ch = (next_[i][1] == 1 ? 'R' : 'L');
				}
				tail++;
				// 找到出口 
				if(nX == n - 1 && nY == m - 1) {
					flag = 1;
					break;
				}
			}
		}
		if (flag == 1) {
			break;
		}
		head++;
	}
	print(tail - 1); 
}

int main() {
	bfs();
	
	return 0;
}

试题 F: 特别数的和

#include<iostream>
using namespace std;
int main() {
	int n;
	cin>>n;
	int sum=0;
	for(int i=1; i<=n; i++) {
		int m=i;
		int flag=0;//判断数字是否符合要求
		int temp;//每次取数字的中间变量
		while(m) {
			temp=m%10;
			if(temp==2||temp==0||temp==1||temp==9) {
				flag=1;
				break;
			} 
			m/=10;
		}
		if(flag==1)
			sum+=i;
	}
	cout<<sum<<endl;
}

记得当时比赛的时候这一道题,一气呵成,可以说是这里面最简单的题目,1s时间直接暴力解题,还是不要眼高手低,以后简单的题目也好好练练

试题 G: 完全二叉树的权值

考察二叉树的基本性质:完全二叉树每一层的节点数量为2^(n-1),到第i层之前所有的二叉树的数量为2^(n)-1,首先判断所有节点的个数,判定二叉树是否是满二叉树,而一颗层数为 17 层的完全二叉树最大节点数为 2^17 - 1 = 131071,已经大于给定的 n 的范围,那么我们计算出每一个节点所属的深度层,再统计就很简单了,因为题目中给的根的深度为 1, 那么我们计算的深度需要往后移一位,即最大深度为 18,下面是代码:

#include <iostream>
#include <cstdio>
using namespace std; 
const int MAX_DEEP = 18;
long long temp[MAX_DEEP + 1];

// 获取下标为 n 的节点的深度 
int getDeep(int n) {
	int res = 0;
	while (n > 0) {
		n /= 2;
		res++;
	} 
	return res;
}

int main() {
	int n, t;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		scanf("%d", &t);
		// 求出当前节点所属的深度层并加入当前深度层节点的权值和中 
		temp[getDeep(i)] += t;
	} 
	int res = 0x80000000, resDeep;
	// 节点权值和最大的深度层 
	for (int i = 1; i <= MAX_DEEP; i++) {
		if (temp[i] > res) {
			res = temp[i];
			resDeep = i;
		}
	}
	cout << resDeep << endl;
	return 0;
}

试题 I: 后缀表达式

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

int value[100010]; 

int gcd(int a, int b) {
	int t;
	while (a) {
		t = a;
		a = b % a;
		b = t;
	}
	return b;
}

int main() {
	int n, d, t;
	int maxx = 0, minn = 0x7fffffff;
	cin >> n;
	for (int i = 0; i < n; i++) {
		scanf("%d", value + i);
		maxx = max(maxx, value[i]);
		minn = min(minn, value[i]);
	}
	sort(value, value + n);
	// 求出所有相邻数字差值的最大公约数 
	d = value[1] - value[0];
	for (int i = 2; i < n; i++) {
		d = gcd(d, value[i] - value[i-1]);
	}
	// 注意公差为 0 的处理 
	if (d == 0) {
		cout << n << endl;
	} else {
		cout << ((maxx - minn) / d + 1) << endl; 
	}
	
	return 0;
} 

公差应该是排序之后求出所有相邻数字之间差值的最大公约数(注意公差为0时间的处理)

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值