zl程序教程

您现在的位置是:首页 >  其他

当前栏目

第十届省赛蓝桥杯b组c++

2023-02-18 16:27:11 时间

?目录

?冲刺蓝桥 距离【第十三届蓝桥杯4月9日省赛】仅剩【03天】 ?

?今日题目:蓝桥杯真题

?刷题一览

往期文章推荐-------0基础算法系列

排序(十大排序) 高精度算法 从0->1入门双指针 前缀和 二分 位运算 区间合并

组队

#include<iostream>
using namespace std;
int one[20] = {97, 92, 0, 0, 89, 82, 0, 0, 0, 95, 0, 0, 94, 0, 0, 0, 98, 93, 0, 0};
int two[20] = {90, 85, 0, 0, 83, 86, 0, 97, 0, 99, 0, 0, 91, 83, 0, 0, 83, 87, 0, 99};
int three[20] = {0, 96, 0, 0, 97, 0, 0, 96, 89, 0, 96, 0, 0, 87, 98, 0, 99, 92, 0, 96};
int four[20] = {0, 0, 0, 80, 0, 0, 87, 0, 0, 0, 97, 93, 0, 0, 97, 93, 98, 96, 89, 95};
int five[20] = {0, 0, 93, 86, 0, 0, 90, 0, 0, 0, 0, 98, 0, 0, 98, 86, 81, 98, 92, 81};
int main()
{
	int ans=0;
	for(int i=0;i<20;i++){
		for(int j=0;j<20;j++){
			if(i==j)	continue;
			for(int k=0;k<20;k++){
				if(i==k||j==k)	continue;
				for(int m=0;m<20;m++){
					if(i==m||j==m||k==m)	continue;
					for(int n=0;n<20;n++){
						if(i==n||j==n||k==n||m==n)	continue;
						if(one[i]+two[j]+three[k]+four[m]+five[n]>ans)
						ans=one[i]+two[j]+three[k]+four[m]+five[n];
					}	
				}	
			}	
		}	
	}
	cout<<ans;
	return 0;
}

年号字串

2019 / 26 = 77 余 17 ,17 对应 Q 77 / 26 = 2 余 25,25 对应 Y 2 / 26 = 0 余 2, 2 对应 B BYQ

数列求值

简单的dp,状态转移方程很好写

#include <iostream>
using namespace std;
int a[20190325];
int main()
{   
	a[1] = a[2] = a[3] = 1;
	for (int i = 4; i <= 20190324; i++)
	{
		a[i] = (a[i - 1] + a[i - 2] + a[i - 3])%10000;//对后4位取模,已经溢出longlong
	}
	cout << a[20190324] << endl;
	return 0;
}

数的分解

#include <iostream>
using namespace std;

bool check(int i, int j, int k)
{
	int w[] = {i, j, k};
	for (int i = 0; i < 3; i ++)
	{
		int n = w[i];
		while(n)
		{
			if(n % 10 == 2 || n % 10 == 4) return false;
			n /= 10;
		}
	}
	return true;
}

int main()
{
	int ans = 0;
	for (int i = 1; i < 2019; i ++)
		for (int j = i + 1; j < 2019; j ++)
			for (int k = j + 1; k < 2019; k ++)	
				if(i + j + k == 2019 && check(i, j, k))
					ans ++;
	
	cout << ans << endl;
	return 0;	
}

迷宫

填空题,直接暴力枚举,采用dfs不剪枝

#include<bits/stdc++.h>
using namespace std;
 
const int n = 10;
char mp[n + 2 ][n + 2];  //二维矩阵存储迷宫 
bool vis[n + 2][n + 2];  //记忆化功能:判断路是否曾经走过,走过那就标个true
						 //如果第二次遇到,说明你进入了死循环,具体见dfs函数 
 
int ans = 0;
int cnt = 0;
 
bool dfs(int i, int j){
	//递归出口 
	if(i < 0 || i > n - 1 || j < 0 || j > n - 1) return true;
	if(vis[i][j]) return false;
	
	cnt++;  //统计dfs使用了多少次 
	vis[i][j] = true;  //标记这块地已经走过
	
	//递归条件 
	if(mp[i][j] == 'L') return dfs(i, j - 1);
	if(mp[i][j] == 'R') return dfs(i, j + 1);
	if(mp[i][j] == 'U') return dfs(i + 1, j);
	if(mp[i][j] == 'D') return dfs(i - 1, j);
	 
}
 
int main(){
	
	//输入每一个格子中的字符:
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			cin >> mp[i][j];
		}
	}	
	
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			memset(vis, 0, sizeof(vis));
			if(dfs(i, j)) ans++;
		}
	}
	
	cout << "ans=" << ans <<", cnt=" << cnt << endl;
	
	return 0;
}

特别数的和

#include<iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    int res=0;
    for(int i=1;i<=n;i++){
        int x=i;
        while(x){
            int t=x%10;//取出个位
            x/=10;//删掉个位
            if(t==2||t==0||t==1||t==9){
                res+=i;
                break;//终止while循环,防止含有多个特别数的值被重复相加
            }
        }
    }
    cout<<res<<endl;
    return 0;
}

完全二叉树的权值

遍历二叉树问题

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long ll;
int a[N];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    int depth=0;
    ll maxx=-N;
    // 双指针,i在前,j在后,
    //i代表每层的第一个数的坐标,j代表每层的最后一个数的坐标
    for(int d=1,i=1;i<=n;i*=2,d++)
    {
        ll sum=0;
        for(int j=i;j<=2*i-1&&j<=n;j++)
        {
            sum+=a[j];
        }
        if(sum>=maxx)
        {
            maxx=sum;
            depth=d;
        }
    }
    cout<<depth<<endl;
    return 0;
}

等差数列

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

const int N = 100010;
int a[N];
int n;

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

int main()
{
    scanf("%d",&n);
    for(int i = 0;i < n;i ++) scanf("%d",&a[i]);
    sort(a,a + n);
    
    int d = 0;
    for(int i = 1;i < n;i ++) d = gcd(d, a[i] - a[i - 1]);
    
    if(!d) printf("%d\n",n);
    else printf("%d\n",(a[n - 1] - a[0]) / d + 1);
    
    return 0;
}

后缀表达式

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
 
typedef long long ll;
 
int n,m,N;
 
int main()
{
    scanf("%d%d",&n,&m);
    N=n+m+1;
    int minz=1e9+1,maxf=-1e9-1;
    int numz=0,numf=0;
    int tmp;
    ll sumnum=0,sumn=0;
    for(int i=0;i<N;i++)
    {
        scanf("%d",&tmp);
        sumnum+=abs(tmp);
        sumn+=tmp;
        if(tmp<0)
        {
            numf++;
            maxf=max(maxf,tmp);
        }
        else
        {
            numz++;
            minz=min(minz,tmp);
        }
    }
    //cout<<numz<<' '<<numf<<endl;
    //cout<<minz<<' '<<maxf<<endl;
    if(numf==m)
        cout<<sumnum<<endl;
    else if(m==0)
        cout<<sumn<<endl;
    else if(numf>m)
    {
        if(numz==0)
            cout<<sumnum+2*maxf<<endl;
        else
            cout<<sumnum<<endl;
    }
    else
    {
        if(numf==0)
            cout<<sumnum-2*minz<<endl;
        else
            cout<<sumnum<<endl;
    }
 
    return 0;
}