第十一届蓝桥杯大赛软件类省赛 C/C++ 大学 B 组 试题+题解

本文详细解析了第十一届蓝桥杯大赛软件类C/C++大学B组的多项竞赛题目,包括门牌制作、既约分数、蛇形填数等。通过题解,介绍了动态规划、数论和模拟等解题方法,展示了C++代码实现。
摘要由CSDN通过智能技术生成

试题 A. 门牌制作

题目描述:计算1-2020中出现了多少次2,注意不是多少个数字出现2。
题解:直接写,送分题
C++ 代码:

#include<iostream>

using namespace std;
int res = 0;  
void count(int x)
{
   
	while(x)
	{
   
		if(x % 10 == 2) res ++;
		x /= 10;
	}
}
int main()
{
   
	for(int i = 0;i <= 2020 ; i ++ )
		count(i);
	cout << res << endl;
	return 0;
}
// 624

试题 B:既约分数

题目描述:求多少个分数使得分子和分母的最大公约数为1,且同时分子和分母均在1-2020之间。
题解: 数论 gcd,补充算法:在algorithm包下有一个函数__gcd(int i,int j) 牛啊,以后再也不傻傻的去写gcd函数了,啦啦啦啦
这里也写一下gcd函数,毕竟比较经典的辗转相减法

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

C++ 代码:

#include <iostream>
#include <algorithm>

using namespace std;
 
int main()
{
   
	int res = 0;
	for(int i = 1; i <= 2020; i ++ )
		for(int j = 1; j <= 2020; j ++ )
			if(__gcd(i, j) == 1) res ++;
	
	cout << res << endl;
	return 0;
}
//2481215 

试题 C. 蛇形填数

题目描述
如下图所示,小明用从 1 开始的正整数“蛇形”填充无限大的矩阵。
1 2 6 7 15 :::
3 5 8 14 :::
4 9 13 :::
10 12 :::
11 :::
:::
容易看出矩阵第二行第二列中的数是 5。请你计算矩阵中第 20 行第 20 列的数是多少 ?
题解: 这题我看大家都是去模拟题目的意思,打印出蛇形矩阵,其实不然,他问的是第20行第20列的数是多少,观察对角线上元素,有一定的规律可循:(前4个对角线上的元素如下)1 5 13 25 可以看出

int res0 = 1;
res1 = 1 = res0 + (1 - 1) * 4;
res2 = 5 = res1 + (2 - 1) * 4;
res3 = 13 = res2 + (3 - 1) * 4;
res4 = 25 = res 3 + (4 - 1) * 4 

找到如下的规律就可直接很快的解决问题了:
C++ 代码:

#include<iostream>

using namespace std;

int main()
{
   
	int res = 1; 
	for(int i = 2; i <= 20; i ++ )
	{
   
		res += 4*(i - 1);
		cout << i << " : " << res << endl; 
	}
	return 0;
}
//761

但是本着精益求精的态度,我们给出模拟蛇形填数的代码
C++ 代码:

#include <iostream>

using namespace std;
const int N = 30;

int a[N][N];

int main() 
{
   

    int n = 21, num = 21 * 21;
    for(int i = 1, cnt = 1; i <= n && cnt <= num; i++) 
	{
   
        if(i & 1) 
		{
   
            for(int x = i, y = 1; x >= 1 && y <= i; x--, y++) 
                a[x][y] = cnt++;
        }
        else 
		{
   
            for(int x = 1, y = i; x <= i && y >= 1; x++, y--) 
                a[x][y] = cnt++;
        }
    }
    printf("%d\n", a[20][20]);
	return 0;
}
// 761

试题 D: 跑步锻炼

题目描述: 小蓝每天都锻炼身体。
正常情况下,小蓝每天跑 1 千米。如果某天是周一或者月初(1 日)为了激励自己,小蓝要跑 2 千米。如果同时是周一或月初,小蓝也是跑 2千米。小蓝跑步已经坚持了很长时间,从 2000 年 1 月 1 日周(含)到 2020 年10 月 1 日周四(含)。请问这段时间小蓝总共跑步多少千米?

题解: 我们打比赛最喜欢的大模拟题,谁知道那个抽风的出题人
C++代码:

#include <iostream>
#include <algorithm> 
#include <cstring> 

using namespace std;

int mouths[13] = {
   0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 
bool isleap(int yr)
{
   
	return yr % 4 == 0 && yr % 100 != 0 || yr % 400 == 0; 
}

int main()
{
   
	int res = 0, sum = 0;
	int week = 5;
	for(int y = 2000;y < 2020; y ++ )
	{
   
		if(isleap(y)) mouths[2] = 29;
		else mouths[2] = 28;
		for(int i = 1; i <= 12; i ++ )
		{
   
			for(int j = 1;j <= mouths[i]; j ++ )
			{
   
				if((j == 1 && week == 0) || j == 1 || week == 0) res ++;
				week ++;
				week %= 7<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

1bu3dong

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

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

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

打赏作者

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

抵扣说明:

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

余额充值