【蓝桥杯】2020 蓝桥杯C++ B组国赛题目集

2020 11-14 蓝桥杯C++ B组国赛题目合集

国赛过去一段时间了,发挥很烂,看了蛮多的题解的,来记录一下这些题。

填空题

扩散(搜索)

很标准的深搜,比赛的时候不知道为什么写了一个一直卡住的

#include<bits/stdc++.h>
#include<queue>
using namespace std;
int n, change[4][2] = {-1, 0, 1, 0, 0, 1, 0, -1};
int vis[10000][10000];
long long cnt;
struct node{
	int x, y, tm;
}s, t;
void dfs(){
	queue<node> q;
	q.push((node){2020 + 2100, 11 + 2100, 0});
	q.push((node){11 + 2100, 14 + 2100, 0});
	q.push((node){2000 + 2100, 2000 + 2100, 0});
	q.push((node){0 + 2100, 0 + 2100, 0});
	vis[2020 + 2100][11 + 2100] = vis[11 + 2100][14 + 2100] = vis[2000 + 2100][2000 + 2100] = vis[0 + 2100][0 + 2100] = 1;
	cnt = 4;
	while(q.size()){
		t = q.front();
		q.pop();
		s.tm = t.tm + 1;
		for(int i = 0; i < 4; i++){
			s.x = t.x + change[i][0];
			s.y = t.y + change[i][1];
			if(vis[s.x][s.y] == 0 && s.tm <= n){
				vis[s.x][s.y] = 1;
				cnt++;
				q.push(s);
			}
		}
	}
	cout << cnt;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	dfs();
	return 0;
}

阶乘约数( 数学)

把2-100的每一个数字进行分解,记录下每一个素因子的个数,每个素因子出现的次数再+1就是它可以选择的种数,把所有的乘起来就是答案了

#include<bits/stdc++.h>
using namespace std;
int a[105];
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int k, x, y;
	for(int i = 0; i <= 100; i++){
		x = i;
		for(int j = 2; j * j <= i; j++){
			while(x % j == 0){
				a[j]++;
				x = x / j;
			}
		}
		if(x != 1) a[x]++;
	}
	long long ans = 1;
	for(int i = 1; i <= 100; i++){
		if(a[i]) ans = ans * (a[i] + 1);
	}
	cout << ans;
	return 0;
}

本质上升序列(字符串处理)

对字符串进行处理,把得到的子序列存到队列里面,然后用map来记录,用一个指针来存下子序列的位置

#include<bits/stdc++.h>
using namespace std;
char s[500] = "tocyjkdzcieoiodfpbgcncsrjbhmugdnojjddhllnofawllbhfiadgdcdjstemphmnjihecoapdjjrprrqnhgccevdarufmliqijgihhfgdcmxvicfauachlifhafpdccfseflcdgjncadfclvfmadvrnaaahahndsikzssoywakgnfjjaihtniptwoulxbaeqkqhfwl";
queue<pair<string, int> > q;
map<string, int> vis;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	long long ans;
	for(int i = 0; i < 200; i++){
		string str;
		str = str + s[i];
		if(vis[str] == 0){
			vis[str] = 1;
			q.push(make_pair(str, i));
			ans++;
		}
	}
	while(q.size()){
		string str = q.front().first;
		int pos = q.front().second;
		q.pop();
		for(int i = pos + 1; i < 200; i++){
			if(s[i] > s[pos] && vis[str + s[i]] == 0){
				vis[str + s[i]] = 1;
				q.push(make_pair(str + s[i], i));
				ans++;
			}
		}
	}
	cout << ans;
	return 0;
}

蛇(搜索)

简单的DFS,搜索四个点,然后没深一层sum++,sum=总面积时,次数+1

#include<bits/stdc++.h>
using namespace std;
int ans, v[5][5], yd[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
void dfs(int x, int y, int sum){
	if(sum == 16){
		ans++;
		return;
	}
	for(int i = 0; i < 4; i++){
		int fx = x + yd[i][0];
		int fy = y + yd[i][1];
		if(fx < 1 || fy < 1 || fy > 4 || fx > 4 || v[fx][fy] == 1) continue;
		v[fx][fy] = 1;
		dfs(fx, fy, sum + 1);
		v[fx][fy] = 0;
	}
}
int main(){
	for(int i = 1; i <= 4; i++){
		for(int j = 1; j <= 4; j++){
			memset(v, 0, sizeof(v));
			v[i][j] = 1;
			dfs(i, j, 1);
		}
	}
	cout << ans;
	return 0;
}

编程题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Why_so?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值