第十届蓝桥杯c/c++省赛B组 A~H题

在这里插入图片描述
在这里插入图片描述

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

暴力递归枚举回溯即可
答案: 490

#include <iostream>
using namespace std;

int a[25][10],ans;
bool st[25];

void dfs(int u,int s)
{
	if(u == 5)
	{
		ans = max(ans,s);
		return;
	}
	
	for(int i = 0; i < 20; i++)
	{
		if(st[i])	continue;
		st[i] = true;
		dfs(u + 1,s + a[i][u]);
		st[i] = false;
	}
}

int main()
{
	for(int i = 0; i < 20; i++)
	{
		int t;
		cin >> t;
		for(int j = 0; j < 5; j++)
			cin >> a[i][j];
	}
	
	dfs(0,0);
	
	cout << ans << endl;
	
	return 0;
}

在这里插入图片描述
其实就是求2019的26进制
答案:BYQ

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

vector<char> ans;

void dfs(int n)
{
	if(!n)	return;
	dfs(n/26);
	char t = 'A' + (n % 26) - 1;
	ans.push_back(t);
}

int main()
{	
	dfs(2019);
	for(auto t : ans)
		cout << t ; 
		
	return 0;
}

在这里插入图片描述
和斐波那契数列类似,注意要取模 10000 就行,取模不影响后四位的正确性
答案:4659

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

typedef long long LL;

int main()
{	
	LL f1,f2,f3,f4;
	f1 = f2 = f3 = 1;
	
	for(int i = 4; i <= 20190324; i++)
	{
		f4 = (f1 + f2 + f3) % 10000;
		f1 = f2;
		f2 = f3;
		f3 = f4;
	}
	
	cout << f4 << endl;
		
	return 0;
}

在这里插入图片描述
直接爆搜就可以了,要注意的是这里要枚举组合数
答案:40785

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

int ans;

bool st[2030];

bool check(int n)
{
	while(n)
	{
		int t = n%10;
		if(t == 2 || t == 4)	return false;
		n /= 10;
	}
	return true;
}

void dfs(int u,int start,int n)
{
	if(n > 2019)	return;
	
	if(u == 3)
	{
		if(n == 2019)	ans++;
		return;
	}
	
	for(int i = start; i <= 2015; i++)
	{
		if(!check(i))	continue;
		if(st[i])	continue;
		st[i] = true;
		dfs(u + 1,i,n + i);
		st[i] = false;
	}
}

int main()
{
	dfs(0,1,0);
	
	cout << ans << endl;
	
	return 0;
}

在这里插入图片描述
输入数据 :

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

思路: 用广度优先搜索解决,因为bfs可以保证搜索到的路径是最短的,储存下路径递归枚举输出路径
答案:DDDDRRURRRRRRRDRRRDDDLDDRDDDDDDDDDDDDRDRDRRUUURRRRDDDDRDRRRRRURRRDRRDDDRRRRUURUUUUUUUULLLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDRDRRRRDRDRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR

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

typedef pair<int,int>PII;

char mp[35][55];
bool st[35][55];
char ans[1500];
PII path[35][55];

char dir[4] = {'D','L','R','U'};
int dx[] = {1,0,0,-1};
int dy[] = {0,-1,1,0};

void printpath(int x,int y,int nextx,int nexty)
{
	if(!nextx && !nexty)	return;
	printpath(path[x][y].first,path[x][y].second,x,y);
	for(int i = 0; i < 4; i++)
		if( x + dx[i] == nextx && y + dy[i] == nexty )
		{
			printf("%c",dir[i]);
			break;
		}	
}

void bfs(int sx,int sy)
{
	queue<PII> q;
	q.push({sx,sy});
	
	while(q.size())
	{
		PII t = q.front();
		q.pop();
		int tx = t.first , ty = t.second;
		st[tx][ty] = true;
		for(int i = 0; i < 4; i++)
		{
			int xx = tx + dx[i] , yy = ty + dy[i];
			if(xx < 0 || yy < 0 || xx >= 30 || yy >= 50 || st[xx][yy] || mp[xx][yy] == '1')
				continue;
				
			q.push({xx,yy});
			
			path[xx][yy] = {tx,ty};
			if(xx == 29 && yy == 49)
			{
				printpath(path[xx][yy].first,path[xx][yy].second,xx,yy);
				exit(0);
			}
		}
	}
	
}

int main()
{
	for(int i = 0; i < 30; i++)
		scanf("%s",mp[i]);

	bfs(0,0);
	
	return 0;
}

在这里插入图片描述
题目链接
直接遍历一遍 1 ~ n ,判断一下就行了

#include<iostream>
using namespace std;
long long ans;
int n;

bool check(int n)
{
	while(n)
	{
		int t = n % 10;
		if(t == 2 || t == 0 || t == 1 || t == 9)	return true;
		n /= 10;
	}
	return false;
}

int main()
{	
	cin >> n;
	
	for(int i = 1; i <= n; i++)
		if(check(i))	ans += i;
	
	cout << ans << endl; 
	
	return 0;
}

在这里插入图片描述
在这里插入图片描述
题目链接
直接用数组存储这颗完全二叉树就行了,但是需要根据数组的 下标映射到对应其在树中的深度,注意和可能会爆int,所以需要开long long来存储

#include <iostream>
using namespace std;

const int N = 100010;

long long tree[N],sum[50];

int lowbit(int x)
{
	return x & -x;
}

int lg(int n)
{
	int v,res = 0;
	while(n)
	{
		v = lowbit(n);
		n -= v;
	}
	while(v)
	{
		v >>= 1;
		res ++;
	} 
	return res;
}

int main()
{
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++)
		cin >> tree[i];
	
	for(int i = 1; i <= n; i++)
	{
		int u = lg(i);
		sum[u] += tree[i];
	}
	
	int maxs = sum[1] , m = lg(n);
	for(int i = 2; i <= m; i++)
		maxs = max(maxs,sum[i]);
	
	for(int i = 1; i <= m; i++)
		if(sum[i] == maxs)
		{
			cout << i << endl;
			break;
		}
	
	return 0;
}

在这里插入图片描述
在这里插入图片描述
题目链接
一道数学题,其实就是找排序后的每相邻两个数的差的最小的最大公约数,这个数就是公差 d ,然后根据 an = a1 + (n-1)d 算出最大的那项是第几项就行

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

const int N = 100010;

int a[N],g[N];

int gcd(int a,int b)
{
	return b ? gcd(b,a % b) : a;
}

int main()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> a[i];
	
	sort(a,a + n);
	
	for(int i = 1; i < n; i++)
		g[i-1] = a[i] - a[i - 1];
	
	int d = 1e9;
	for(int i = 1; i < n - 1; i++)
		d = min(d,gcd(g[i-1],g[i]));
	if(d)
		cout << (a[n-1] - a[0]) / d + 1 << endl;
	else
		cout << n << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值