蓝桥杯第十届C/C++ B 省赛题目及题解

试题 A:组队

 

 

本题思路:基本就是考篮球常识,一个人只能打一个位置,不能同时打两个位置,只要找出每个位置最强的那个或则次强的就行。

答案:490; 1号位:17;2号位:10 3号位:15;4号位:11;5号位:12。

再后来老师讲解时叫我们可以尝试用贪心代码去做;我的代码如下:

 

#include<stdio.h>
#include<Windows.h>
int ans;
int b[21];
int a[21][6] = {
		0,0,0,0,0,0,
		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
};
void init()
{
	for (int i = 0; i < 21; i++)
	{
		b[i] = 0;
	}
}
void f(int i)
{
	if (i > 5)
		return;
	int res = 0;          //表示此时最大
	for (int j = 1; j <= 20; j++)
	{
		if (b[j] == 0 && a[j][i] > a[res][i])
		{
			res = j;
		}
	}

	b[res] = 1;
	printf("%d ", res);
	ans += a[res][i];
	f(i + 1);
}
int main()
{
	init();
	f(1);
	printf("%d\n", ans);
	system("pause");
	return 0;
}

 

试题 B:年号字串

 

 

第一种:直接暴力用Excel表格来做,大一参加比赛的时候就是用这样的方法做出来的。 答案:BYQ

 

第二种:思路:参考二进制,可以把本题看作是一个26进制题目。

代码:(这个代码不是完整的26进制,测试 52 时答案并不正确,暂时还没能解决这个问题!!!!)

 

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

int main()
{
	string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	string res;
	int n;
	cin >> n;

	while (n)
	{
		int t = n % 26;
		res += str[t - 1];
		n /= 26;
	}
	for (int i = res.size() - 1; i >= 0; i--)
		cout << res[i];
	system("pause");
	return 0;
}

试题 C: 数列求值

 

思路:本题是fibnacci数列的变题,如果用暴力解决可能会超时出错,这里可以思考用动态规划来做。

最后取每个数的最后4位数即可。注意:用A的后四位数来加上B的后四位数,不会影响A和B结果的后四位数。

举例:462354+765432=1227786        345986+4423675=4769661    

本题答案:

#include<iostream>
using namespace std;
int main()
{
	int n;
	cin >> n;
	int a = 1, b = 1, c = 1;
	for (int i = 4; i <= n; i += 3)
	{
		a = (a + b + c) % 10000;
		b = (b + a + c) % 10000;
		c = (c + a + b) % 10000;
	}
	if (n % 3 == 0) 
		cout << c << endl;
	else if (n % 3 == 1)
		cout << a << endl;
	else
		cout << b << endl;
	system("pause");
	return 0;
}

 

试题 D: 数的分解 

 

 

答案:40785 

每层循环从上一层循环的下一个数开始循环可以避免出现重复的结果。

 

#include<iostream>
#include<string>
using namespace std;
bool f(int n)//判断三个数的位数中是否不含2和4
{
	string str = to_string(n);
	for (int i = 0; i < str.size(); i++)
	{
		if (str[i] == '2' || str[i] == '4')
			return false;
	}
	return true;
}
int main()
{
	int n;
	cin >> n;
	int ans = 0;
	for (int i = 1; i < n; i++)
	{
		for (int j = i + 1; j < n; j++)
		{
			for (int k = j + 1; k < n; k++)
			{
				if (i + j + k == n)
				{
					if (f(i) && f(j) && f(k))
					{
						cout << i << " " << j << " " << k << endl;
						ans++;
					}
				}
			}
		}
	}
	cout << ans << endl;
	system("pause");
	return 0;
}

 

试题 E:迷宫

 

/*
测试数据
30 50 
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
*/
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<cstring>
using namespace std;
const int N = 55;
int n, m;
string g[N];
int dist[N][N];
int dx[4] = { 1,0,0,-1 }, dy[4] = { 0,-1,1,0 };
char dir[4] = { 'D','L','R','U' };

void bfs()
{
	queue<pair<int, int>> q;
	memset(dist, -1, sizeof dist);
	dist[n - 1][m - 1] = 0;
	q.push({ n - 1,m - 1 });
	while (q.size())
	{
		auto t = q.front();
		q.pop();

		for (int i = 0; i < 4; i++)
		{
			int x = t.first + dx[i], y = t.second + dy[i];
			if (x >= 0 && x < n && y >= 0 && y < m && dist[x][y] == -1  && g[x][y] == '0')
			{
				dist[x][y] = dist[t.first][t.second] + 1;
				q.push({ x,y });
			}
		}
	}

}
int main()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++) cin >> g[i];
	bfs();

	cout << dist[0][0] << endl;
	string  res;
	int x = 0, y = 0;
	while (x != n - 1 || y != m - 1)
	{
		for (int i = 0; i < 4; i++)
		{
			int nx = x + dx[i], ny = y + dy[i];
			if (nx >= 0 && nx < n && ny >= 0 && ny < m && g[nx][ny] == '0')
			{
				if (dist[x][y] == 1 + dist[nx][ny])
				{
					x = nx, y = ny;
					res += dir[i];
                                        break;
				}
			}
		}
	}
	cout<<res<<endl;
	system("pause");
	return 0;
}

答案:186(距离)
DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDRDRRURRUURRDDDDRDRRRRRURRRDRRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDRDRRRRDRDRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR

 

试题 F:特别数的和

 

 

//这个题就比较简单了,直接循环,判断每个数中是否含有2019这个四个数字,10000完全不用考虑超时
#include<iostream>
#include<string>
using namespace std;
bool check(int x)
{
	string str = to_string(x);
	for (int i = 0; i < str.size(); i++)
	{
		if (str[i] == '2' || str[i] == '0' || str[i] == '1' || str[i] == '9')
			return true;
	}
	return false;
}
int main()
{
	int n;
	int ans = 0;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		if (check(i))
		{
			//cout << i << endl;
			ans += i;
		}
	}
	cout << ans << endl;
	system("pause");
	return 0;
}

 

//第二种写法,思路跟上面的一样,只是check使用的方法不同
#include<iostream>
using namespace std;
bool check(int x)
{
	while (x)
	{
		int t = x % 10;
		if (t == 2 || t == 0 || t == 1 || t == 9)
			return true;
		x /= 10;
	}
	return false;
}
int main()
{
	int n;
	int ans = 0;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		if (check(i)) ans += i;
	}
	cout << ans << endl;
	system("pause");
	return 0;
}

 

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

 

 

 这里先回忆一下满二叉树和完全二叉树的定义:

满二叉树:一颗深度为k且有2^k - 1个结点的二叉树称为满二叉树。

完全二叉树:对满二叉树的结点进行编号,约定编号从根结点起,自上而下,自左而右。则深度为k的,有n个结点的二叉树,当且仅当每一个结点都与深度为k的满二叉树中编号从1至n的结点一一对应时,称之为完全二叉树。

从定义可知,满二叉树是完全二叉树的一种特殊形态,即如果一颗二叉树是满二叉树,则它必是完全二叉树。

 

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

int main()
{
	int n;
	scanf("%d", &n);
	
	int m;
	long long max_t, sum;//分表表示最大值,和各层的值的总和 
	int ans, d = 0, res = 0;//分别表示答案,和深度,每层的个数 
	for(int i = 0; i < n; i++)
	{
		scanf("%d", &m);
		
		if(i == 0)
		{
			max_t = m;
			ans = 1;
			d++; 
		}
		else
		{
			sum += m;
			res++;
	    }
	    
	    if(res == pow(2, d) || i == n - 1)
	    {
	    	if(sum > max_t)
			{
				max_t = sum;
				ans = d + 1;
		    }
		    res = 0;
		    sum = 0;
		    d++;
		}
	}
	
	cout << ans << endl;
	return 0;
} 

 

 试题 H:等差数列

 

 

分析:等差数列有首项a1和公差d,由于an = a1 + (n - 1) * d,即需要知道该数列有多少项那么首项得确定a1和d,因为所有输入的数都是该数列,那么我们把每两个做差,那么差的最小公约数即为最大公差,并且保证是以最大的数来求出数列的项数最小。

 

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

const int N = 1e5;
int a[N];
int n;

int gcd(int a,int b)
{
	if(b == 0) return a;
	return gcd(b, a % b);
}

int main()
{
	scanf("%d", &n);//输入较多,用scanf
	int d;
	for(int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
    } 
    
    sort(a, a + n);
    
    for(int i = 1; i < n; i++)
    {
		if(i == 1) d = a[i] - a[i - 1];
		else d = gcd(d, a[i] - a[i - 1]);
	}
    
    //an = a1 + (n - 1) * d,n = (an - a1) / d + 1;
	
	printf("%d\n", (a[n - 1] - a[0]) / d + 1); 
    return 0;
}

 后面的题目有点难度啊~~,暂时还不能理解~~~

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值