第十一届蓝桥杯省赛真题(C/C++大学B组)

试题A :门牌制作

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

const int N = 100000;
int arr[N];

int main()
{
	int ans = 0,t;
	for(int i = 1;i <= 2020;i++)
	{
		t = i;
		while(t > 0)
		{
			if(t % 10 == 2) ans++;
			t /= 10;
		}
	}
	cout<<ans<<endl;
	
	return 0;
}

试题B :既约分数

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

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

int main()
{
	int ans = 0;
	for(int i = 1;i <= 2020;i++)
	{
		for(int j = 1;j <= 2020;j++)
		{
			if(gcd(i,j) == 1) ans++;
		}
	}
	cout<<ans<<endl;
	
	return 0;
}

试题C :蛇形填数

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

int arr[100][100];

int main()
{
	int sum = 1; 
	for(int i = 0;i < 50;i++)
	{
		//奇数,行-,列+ 
		if(i % 2 == 1)
		{
			for(int x=i,y=1;x >= 0 && y <= i;x--,y++)
				arr[x][y] = sum++;
		}
		//偶数,行+,列-
		else
		{
			for(int x=1,y=i;x <= i && y >= 0;x++,y--)
				arr[x][y] = sum++;
		} 
	}
	cout<<arr[20][20]<<endl;
	return 0;
}
相关推荐
快乐的划水a3 小时前
组合模式及优化
c++·设计模式·组合模式
星星火柴9364 小时前
关于“双指针法“的总结
数据结构·c++·笔记·学习·算法
艾莉丝努力练剑5 小时前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法
Cx330❀6 小时前
【数据结构初阶】--排序(五):计数排序,排序算法复杂度对比和稳定性分析
c语言·数据结构·经验分享·笔记·算法·排序算法
阿巴~阿巴~7 小时前
深入解析C++ STL链表(List)模拟实现
开发语言·c++·链表·stl·list
..过云雨7 小时前
01.【数据结构-C语言】数据结构概念&算法效率(时间复杂度和空间复杂度)
c语言·数据结构·笔记·学习
旺小仔.8 小时前
双指针和codetop复习
数据结构·c++·算法
jingfeng5148 小时前
C++ STL-string类底层实现
前端·c++·算法
郝学胜-神的一滴8 小时前
基于C++的词法分析器:使用正则表达式的实现
开发语言·c++·程序人生·正则表达式·stl
谱写秋天9 小时前
在STM32F103上进行FreeRTOS移植和配置(STM32CubeIDE)
c语言·stm32·单片机·freertos