蓝桥杯——2021第十二届C/C++真题[省赛][B组]_第十二届蓝桥杯c++b组-程序员宅基地

技术标签: 算法  # C/C++蓝桥杯真题  c++  c语言  蓝桥杯  数据结构  

目录

卡片

直线

货物摆放

路径

空间

砝码称重

时间显示 

杨辉三角数 

 双向排序

括号序列 


卡片

思路:这道题咋一看给人一种挺难的感觉,其实很简单,就是一个数的每位遍历。

#include<iostream>
using namespace std;
int main()
{
	int a[10] = { 0 };
	int t = 0;
	for (int i = 0; i <= 9; i++)
	{
		a[i] = 2021;
	}
	for ( t = 1; t <= 9900; t++)
	{
		int i = t;
		while (i > 0)
		{
			a[i % 10]--;
			if (a[i % 10] <= 0)
			{
				cout << t << endl;
				return 0;
			}
			i = i/ 10;
		}
	}
	
}

答案:3181 

直线

两点式直线方程:
(y1-y2) * x +(x2-x1) * y +( x1 * y2 - x2 * y1)=0

思路:先存储所有的坐标 ,遍历所有的坐标组获得直线Ax+By+C=0的A,B,C并使用gcd约分最后再利用set去重,最后再加上垂直于x轴和y轴的数.

#include<iostream>
#include<set>
using namespace std;
typedef pair<double, double> PII;
typedef pair<PII, double> PIII;
set<PIII> ss;
int gcd(int a, int b)
{
	if (b == 0) return a;
	return gcd(b, a % b);
}
int main()
{
	for (int x1 = 0; x1 <= 19; x1++)
	{
		for (int y1 = 0; y1 <= 20; y1++)
		{
			for (int x2 = 0; x2 <= 19; x2++)
			{
				for (int y2 = 0; y2 <= 20; y2++)
				{
					if (x1 != x2 && y1 != y2)
					{
						double a = x2 - x1;
						double b = y1 - y2;
						double c = y2 * x1 - x2 * y1;
						double m = gcd(gcd(a, b), c);
						ss.insert({ { a / m,b / m }, c / m });

						//ss.insert(a);
					}
				}
			}
		}
	}
	cout << ss.size() + 20 + 21;
	return 0;
}

答案:40257 

货物摆放

思路:这道题是填空题,直接纯暴力法,不过要稍微优化下,我们可以通过减少for循环或者用缩小循环的范围即可.这道题就是拆解成三个因子(a,b,c),要保持a<=b<=c;可以通过sqrt来缩小循环范围

#include<iostream>
using namespace std;
#define n 2021041820210418
//n=a*b*c
typedef long long ll;
int ants = 0;
int main()
{
	for (ll a = 1; a * a * a <= n; a++)
	{
		if (n % a == 0)
		{
			for (ll b = a; a * b * b <= n; b++)
			{
				if (n / a % b == 0)
				{
					ll c = n / a / b;
					if (a == b && a == c)ants = 0;
					else if (a == b || a == c || c == b) ants += 3;
					else ants += 6;
				}
			}
		}
	}
	cout << ants << endl;
	return 0;
}

这个是我在网上看的一种解法,把a,b,c放入到一个集合中,最近几年频繁使用这个set,所以尽可能多练练 

#include<iostream>
#include<cmath>
#include<set>
using namespace std;
#define n 2021041820210418
//n=a*b*c
int ants = 0;
typedef long long ll;
int main()
{
	ll end = sqrt(n);
	for (ll a = 1; a <= end; a++)
	{
		if (n % a == 0)
		{
			ll nn = n / a;
			ll end1 = sqrt(nn);
			for (ll b = a; b <= end1; b++)
			{
				if (nn % b == 0)
				{
					ll c = nn / b;
					if (c >= b)
					{
						set<int> s;
						s.insert(a);
						s.insert(b);
						s.insert(c);
						if (s.size() == 1)ants += 1;
						else if (s.size() == 2) ants += 3;
						else if(s.size()==3) ants += 6;
					}
				}
			}
		}
	}
	cout << ants << endl;
	return 0;
}

答案:2430 

路径

 思路:这道题直接就用floyd算法跑就行,反正填空题超时也不怕,三层循环等个几十秒就出来了。迪杰斯特拉写着太麻烦了。

#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
#define INT 0x3f3f3f3f
ll Edge[2022][2022];
int gcd(int a, int b)//最大公约数
{
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
int lcm(int a, int b)//最小公倍数
{
	int c = a * b;
	return c / gcd(a, b);
}
int main()
{
	//初始化
	memset(Edge, INT, sizeof(Edge));
	for (int i = 1; i <= 2021; i++)
	{
		for (int j = 1; j <= 2021; j++)
		{
			if (i == j)Edge[i][j] = 0;
			else {
				if (abs(i - j) <= 21)//判断差的绝对值是否小于等于21
				{
					Edge[i][j] = lcm(i, j);
				}
			}
		}
	}
//floyd算法核心
	for (int k = 1; k <= 2021; k++)
	{
		for (int i = 1; i <= 2021; i++)
		{
			for (int j = 1; j <= 2021; j++)
			{
				if (Edge[i][j] > Edge[i][k] + Edge[k][j])
				{
					Edge[i][j] = Edge[i][k] + Edge[k][j];
				}
			}
		}
	}
	cout << Edge[1][2021];
	return 0;
}

答案:10266837 

空间

思路:

1MB=1024KB  1KB=1024B   1B=8b

  • 答案:((256*1024)*1024)/ 4== 67108864

砝码称重

输入样例

3
1 4 6 

输出样例

10 

思路:

代码

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 110, M = 200010, B = M / 2;
int n, m;
int w[N];
bool f[N][M];
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin>>w[i], m += w[i];
	f[0][B] = true;
	for (int i = 1; i <= n; i++)
	{
		for (int j = -m; j <= m; j++)
		{
			f[i][j+B] = f[i - 1][j+B];
			if (j - w[i] >= -m) f[i][j+B] |= f[i - 1][j - w[i]+B];
			if (j + w[i] <= m)f[i][j+B] |= f[i - 1][j + w[i]+B];

		}
	 }
	int res = 0;
	for (int j = 1; j <= m; j++)
	{
		if (f[n][j + B])
		{
			res++;
		}
	}
	cout<<res<<endl;
	return 0;
}

我还看到一个特别好理解的思路:其时当发现一个重量可以得负数,再和以后的状态做加减转化时,正数减去也能代表负数,
如 有砝码 2 1 3
前俩可以拼凑出的状态 1 2 3 -1,
3 + (-1)和 3 - 1 效果是一样的,所以负的重量状态抛弃掉最后结果也是不变的

#include<iostream>
using namespace std;
int dp[105][100005];
int main()
{
	int n;
    cin >> n;
	dp[0][0] = 1;
	for (int i = 1;i <= n;i++)
	{
		int a;cin >> a;
		for (int j = 0;j <= 100000;j++)
		{
			if (dp[i-1][j])
			{
				dp[i][j] = 1;
				dp[i][j + a] = 1;
				dp[i][abs(j - a)] = 1;
			}
		}
	}
	int ans = 0;
	for (int i = 1;i <= 100000;i++)if (dp[n][i]) ans++;
	cout << ans;
}

时间显示 

 输入样例

 2
46800999
1618708103123

输出样例

13:00:00
01:08:23 

思路:读清题目说的是毫秒,/1000换成秒,因为只要求最后一天的时分秒,所以%24*60*60就是去掉除了最后一天的前面所有天数;剩下的就是求时分秒了,很简单

#include<bits/stdc++.h>
using namespace std;

int main()
{
    long long int n;
    cin>>n;
    n=n/1000;
    n=n%86400;//去掉除了最后一天的前面的天数;24*60*60
    int h,m,s;
    h=n/3600;//求得最后一天的小时
    n=n%3600;
    m=n/60;//分钟
    s=n%60;//秒数
    printf("%02d:%02d:%02d",h,m,s); //输出时间常用的形式,不用判断了
    return 0;
}

杨辉三角数 

输入样例

2
3

输出样例

8
13 

思路:这题主要考数学思维;首先通过观察,杨辉三角左右对称,题目要求找到数N的最先出现的地方,所以我们只要看左边就可以了.

我们如果单单通过观察行和列这样枚举,上面给的数据是10亿,是个很庞大的数据,我们能不能斜着来分析此问题囊? 话不多说直接上图

 我们会发现第一个斜行是1=C(0,0),第二斜行 2=C(2,1),,,,,;以此得到第i斜行的第一个数是C(2*i,i);

下面我们来确定10亿是在哪行,考试时可以用电脑自带的计算机计算,计算得出只要对其前16行进行枚举就可以。

我们从第16斜行开始枚举,出现等于n的数就返回其位置,这里n的位置我们可以这样确定:r为行,k为斜行,然后通过r*(r+1)/2计算它前面的行的总数再加上它所在行的前面的数k+1.对于查找过程中我们发现斜行是按升序来着,可以通过采用二分的方法进行查找,避免超时。

 假如我们推个20;r=6,k=3;n=6*(6+1)/2+3+1=25

代码

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
int n;
LL C(int a, int b)   //计算C(a,b)
{
    LL res = 1;
    for(int i = a, j = 1; j <= b; i --, j ++)
    {
        res = res * i / j;
        if(res > n)
            return res;     // 大于n已无意义,且防止爆LL
    }
    return res;
}
bool check(int k)
{
   
    int l = 2 * k, r = max(n,l);
    while(l < r)
    {
        int mid = l + r >> 1;
        if(C(mid, k) >= n) r = mid;
        else l = mid + 1;
    }
    if(C(r, k) != n)
        return false;
    cout << 1ll*(r + 1) * r / 2 + k + 1 << endl;
    return true;
}
int main()
{
    cin >> n;
    // 从第16斜行枚举
    for(int i = 16; ; i--)
        if(check(i))
            break;
    return 0;
}

 视频版:

第十二届蓝桥杯C++ B组讲解_哔哩哔哩_bilibili

 双向排序

输入样例

3 3
0 3
1 2
0 2 

输出样例

3 1 2 

思路:感觉最后两道题都挺难的,都是考思维的,对于我这个小白真是太不友好了,只会个sort暴力排序偷几分,这个代码我就不放在这里面了.找了acw的视频看了下,条理清晰了些,视频放在这里:

第十二届蓝桥杯C++ B组讲解_哔哩哔哩_bilibili

下面我附上一位大佬写的博客,跟这个视频是相匹配的大家可以去看看,我就不必要再写了,总结的太详细啦.

蓝桥杯 I.双向排序_Jozky86的博客-程序员宅基地_蓝桥杯双向排序

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 100010;
int n, m;
PII stk[N];
int ans[N];
int main()
{
    scanf("%d%d", &n, &m);
    int top = 0;
    while (m -- )
    {
        int p, q;
        scanf("%d%d", &p, &q);
        if (!p)//操作1 
        {
            while (top && stk[top].x == 0) 
				q = max(q, stk[top -- ].y);//出现连续的操作1,我们取最大 
            while (top >= 2 && stk[top - 1].y <= q) 
			//如果当前的操作1比上一次的操作1范围大,则将上一次操作1和操作2删除 
				top -= 2;
            stk[ ++ top] = {0, q};//存本次最佳操作 
        }
        else if (top)//操作2 &&且操作1已经进行过(操作二第一个用没效果) 
        {
            while (top && stk[top].x == 1) q = min(q, stk[top -- ].y);
            while (top >= 2 && stk[top - 1].y >= q) top -= 2;
            stk[ ++ top] = {1, q};
        }
    }
    int k = n, l = 1, r = n;
    for (int i = 1; i <= top; i ++ )
    {
        if (stk[i].x == 0)//如果是操作1 
            while (r > stk[i].y && l <= r) ans[r -- ] = k -- ;//移动r值 ,并赋值 
        else
            while (l < stk[i].y && l <= r) ans[l ++ ] = k -- ; 
        if (l > r) break;
    }
    if (top % 2)
        while (l <= r) ans[l ++ ] = k -- ;
    else
        while (l <= r) ans[r -- ] = k -- ;

    for (int i = 1; i <= n; i ++ )
        printf("%d ", ans[i]);
    return 0;
}

括号序列 

 老样子这题还是不完全会,又是用暴搜在测试点骗了部分分。

既然咱做不出来就要去学习大佬的做法和思路。

博客:12届蓝桥杯省赛c++b组 J题 括号序列_thejohn2020的博客-程序员宅基地_蓝桥杯括号序列 

视频:第十二届蓝桥杯C++ B组讲解_哔哩哔哩_bilibili

代码

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
using LL=long long;
const int N = 5005;
int f[N][N];
int mod=1e9+7;
string s;
int n;
LL get(){
    memset(f,0,sizeof f);
    f[0][0]=1;
    for(int i=1;i<=n;i++){
        if(s[i-1]=='('){
            for(int j=1;j<=n;j++)
                f[i][j]=f[i-1][j-1];
        }
        else{
            f[i][0]=(f[i-1][1]+f[i-1][0])%mod;
            for(int j=1;j<=n;j++)
                f[i][j]=(f[i-1][j+1]+f[i][j-1])%mod;
        }
    }
    for(int i=0;i<=n;i++)
        if(f[n][i])
            return f[n][i];
    return -1;
}
int main(){
    cin>>s;
    n=s.size();
    LL x=get();
    reverse(s.begin(),s.end());
    for(int i=0;i<n;i++){
        if(s[i]==')')
            s[i]='(';
        else
            s[i]=')';
    }
    LL y=get();
    cout<<(x*y)%mod;
}

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_58367586/article/details/123882633

智能推荐

while循环&CPU占用率高问题深入分析与解决方案_main函数使用while(1)循环cpu占用99-程序员宅基地

文章浏览阅读3.8k次,点赞9次,收藏28次。直接上一个工作中碰到的问题,另外一个系统开启多线程调用我这边的接口,然后我这边会开启多线程批量查询第三方接口并且返回给调用方。使用的是两三年前别人遗留下来的方法,放到线上后发现确实是可以正常取到结果,但是一旦调用,CPU占用就直接100%(部署环境是win server服务器)。因此查看了下相关的老代码并使用JProfiler查看发现是在某个while循环的时候有问题。具体项目代码就不贴了,类似于下面这段代码。​​​​​​while(flag) {//your code;}这里的flag._main函数使用while(1)循环cpu占用99

【无标题】jetbrains idea shift f6不生效_idea shift +f6快捷键不生效-程序员宅基地

文章浏览阅读347次。idea shift f6 快捷键无效_idea shift +f6快捷键不生效

node.js学习笔记之Node中的核心模块_node模块中有很多核心模块,以下不属于核心模块,使用时需下载的是-程序员宅基地

文章浏览阅读135次。Ecmacript 中没有DOM 和 BOM核心模块Node为JavaScript提供了很多服务器级别,这些API绝大多数都被包装到了一个具名和核心模块中了,例如文件操作的 fs 核心模块 ,http服务构建的http 模块 path 路径操作模块 os 操作系统信息模块// 用来获取机器信息的var os = require('os')// 用来操作路径的var path = require('path')// 获取当前机器的 CPU 信息console.log(os.cpus._node模块中有很多核心模块,以下不属于核心模块,使用时需下载的是

数学建模【SPSS 下载-安装、方差分析与回归分析的SPSS实现(软件概述、方差分析、回归分析)】_化工数学模型数据回归软件-程序员宅基地

文章浏览阅读10w+次,点赞435次,收藏3.4k次。SPSS 22 下载安装过程7.6 方差分析与回归分析的SPSS实现7.6.1 SPSS软件概述1 SPSS版本与安装2 SPSS界面3 SPSS特点4 SPSS数据7.6.2 SPSS与方差分析1 单因素方差分析2 双因素方差分析7.6.3 SPSS与回归分析SPSS回归分析过程牙膏价格问题的回归分析_化工数学模型数据回归软件

利用hutool实现邮件发送功能_hutool发送邮件-程序员宅基地

文章浏览阅读7.5k次。如何利用hutool工具包实现邮件发送功能呢?1、首先引入hutool依赖<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.19</version></dependency>2、编写邮件发送工具类package com.pc.c..._hutool发送邮件

docker安装elasticsearch,elasticsearch-head,kibana,ik分词器_docker安装kibana连接elasticsearch并且elasticsearch有密码-程序员宅基地

文章浏览阅读867次,点赞2次,收藏2次。docker安装elasticsearch,elasticsearch-head,kibana,ik分词器安装方式基本有两种,一种是pull的方式,一种是Dockerfile的方式,由于pull的方式pull下来后还需配置许多东西且不便于复用,个人比较喜欢使用Dockerfile的方式所有docker支持的镜像基本都在https://hub.docker.com/docker的官网上能找到合..._docker安装kibana连接elasticsearch并且elasticsearch有密码

随便推点

Python 攻克移动开发失败!_beeware-程序员宅基地

文章浏览阅读1.3w次,点赞57次,收藏92次。整理 | 郑丽媛出品 | CSDN(ID:CSDNnews)近年来,随着机器学习的兴起,有一门编程语言逐渐变得火热——Python。得益于其针对机器学习提供了大量开源框架和第三方模块,内置..._beeware

Swift4.0_Timer 的基本使用_swift timer 暂停-程序员宅基地

文章浏览阅读7.9k次。//// ViewController.swift// Day_10_Timer//// Created by dongqiangfei on 2018/10/15.// Copyright 2018年 飞飞. All rights reserved.//import UIKitclass ViewController: UIViewController { ..._swift timer 暂停

元素三大等待-程序员宅基地

文章浏览阅读986次,点赞2次,收藏2次。1.硬性等待让当前线程暂停执行,应用场景:代码执行速度太快了,但是UI元素没有立马加载出来,造成两者不同步,这时候就可以让代码等待一下,再去执行找元素的动作线程休眠,强制等待 Thread.sleep(long mills)package com.example.demo;import org.junit.jupiter.api.Test;import org.openqa.selenium.By;import org.openqa.selenium.firefox.Firefox.._元素三大等待

Java软件工程师职位分析_java岗位分析-程序员宅基地

文章浏览阅读3k次,点赞4次,收藏14次。Java软件工程师职位分析_java岗位分析

Java:Unreachable code的解决方法_java unreachable code-程序员宅基地

文章浏览阅读2k次。Java:Unreachable code的解决方法_java unreachable code

标签data-*自定义属性值和根据data属性值查找对应标签_如何根据data-*属性获取对应的标签对象-程序员宅基地

文章浏览阅读1w次。1、html中设置标签data-*的值 标题 11111 222222、点击获取当前标签的data-url的值$('dd').on('click', function() { var urlVal = $(this).data('ur_如何根据data-*属性获取对应的标签对象

推荐文章

热门文章

相关标签