蓝桥杯第十届省赛B组c/c++

试题A——组队
在这里插入图片描述
在这里插入图片描述
分析:选出每个位值最高,且保证每个人只对应一个位置
答案:490

试题B——年号字串
在这里插入图片描述
分析:26进制
答案:BYQ

代码:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
	int n = 2019;
	string res = "";
	while(n)
	{
		res += (n % 26 - 1 + 'A');
		n /= 26;
	} 
	reverse(res.begin(),res.end());
	cout << res;
	return 0;
}

试题C——数列求值
在这里插入图片描述
答案:4659

代码:

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
	int a = 1, b = 1, c = 1, d, i = 3;
	while (i != 20190324)
	{
		d = (a + b + c) % 10000;
		a = b, b = c, c = d;
		i++;
	}
	cout << d << endl;
	return 0;
}

试题D——数的分解
在这里插入图片描述
代码:40785

代码:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int res;
bool check(int x)
{
	while(x)
	{
		if(x % 10 == 2 || x % 10 == 4)
		return false;
		x /= 10;
	} 
	return true;
}
int main()
{
	for(int i = 1;i <= 2019;i++)
	{
		if(check(i))
		for(int j = i + 1;i +  j<= 2019;j++)
		{
			if(check(j))
			for(int k = j + 1;i + j + k <= 2019;k++)
			{
				if(check(k) && i + j + k == 2019)
				res++;
			}
		}
	}
	cout << res;
	return 0;
}

试题E:迷宫
在这里插入图片描述
答案:DDUDUDURUUDRDUULUDLUDURLURUUDURURUUDUDURDURDRLRDRRURLUURLDDURDDRLLUDURDLDDULLRDUDLUDRDLRDLUDLUUURDLDLUDUDUDUURLUDLRDUUUUUDUURUDLDLRDLUDRURURLRLRDULUUURDLLLRDLUULDDLLRDLUDLDRUDUDLDLDLLUDLUUDLDLLRLULDLULDLLRDLDLUDRLDDLRLLLDLLDDLDLLDDULDUDDRLLUDDDLDDLDLULDDLLDLUDLDULDLDDRDLDDLDULDURDULDDDDUURLDLDLDUDDURLURLDLDLDLDRRLDDLDULLLUUURLLLULDUUURDDLLUDLDRDDUURDDLULDUUDRULRLDLULUUDURRURDLLLUDRDLUURLDULURDUUDURURRLRLUURUUDRDRRDRUUDDUUULULURUURUUUURURRURUURDRURRDRURUUUURDURUURLRRURRURURURURURUURDRRLURLURUDRDUUURUUDUURLUURDRURURDUUUURDRRURURRRUULDRLRURUUURLRDURUURLUDUURLURDUURLRULUULUURURURRURURURURUURRULURRURRULRURLRRRRUURRLRUUURLURLRLRUUURURRDLRURRURURRRLRURRRURURRRURRUULULR

代码:

#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;

//D<L<R<U
typedef pair<int, int> PII;

char s[35][55];
int n = 30, m = 50;
int dx[] = {1, 0, 0, -1}, dy[] = {0, -1, 1, 0};
char dir[] = {'D', 'L', 'U', 'R'};
bool vis[30][50];

string bfs()
{
	queue<PII> q;
	vis[0][0] = true;
	q.push({0, 0});
	string ans = "";
	while (q.size())
	{
		PII t = q.front();
		q.pop();
		if (t.first == n - 1 && t.second == m - 1) return ans;
		for (int i = 0; i < 4; i++)
		{
			int tx = t.first + dx[i], ty = t.second + dy[i];
			if (tx < 0 || tx > 30 || ty < 0 || ty > 50 || s[tx][ty] == '1' || vis[tx][ty]) continue;
			q.push({tx, ty});
			vis[tx][ty] = true;
			ans.push_back(dir[i]);
		}
	}
	return "None";
}

int main()
{
	for (int i = 0; i < 30; i ++) cin >> s[i];
	cout << bfs() << endl;
	return 0;
}

试题F——特别数的和
在这里插入图片描述
代码:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int res;
int n;
bool check(int x)
{
	while(x)
	{
		if(x % 10 == 2 || x % 10 == 0 || x % 10 == 1 || x % 10 == 9)
		return true;
		x /= 10;
	}
	return false;
}
int main()
{
	cin >> n;
	for(int i = 1;i <= n;i++)
	{
		if(check(i))
		res += i;
	}
	cout << res;
	return 0;
}

试题G:完全二叉树的权值
在这里插入图片描述
代码:

#include<bits/stdc++.h>
using namespace std;
int N;
long long  res = INT_MIN,ans;
int a[100010];
int main()
{
    cin >> N;
    int step = 1,ans;
    for(int i = 1;i <= N;i++)
    cin >> a[i];
    for(int i = 1;i <= N;i *= 2)
    {
        long long sum = 0;
        for(int j = i;j <= 2 * i - 1 && j <= N;j++)
        {
            sum += a[j];
        }
        if(sum > res)
        {
            res = sum;
            ans = step;
        }
        step ++;
    }
    cout << ans;
    return 0;
}

试题H:等差数列
在这里插入图片描述
代码:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int N = 100010; 
int res,n;
int a[N];
int gcd(int a,int b)
{
	return b == 0 ? a : gcd(b,a % b);
}
int main()
{
	cin >> n;
	for(int i = 0;i < n;i++)
	cin >> a[i];
	sort(a,a + n);
	int d =  a[0];
	for(int i = 1;i < n;i++)
	{
		d = gcd(d,a[i]);
	}
	if(d == 0) cout << n;
	else  cout << (a[n - 1] - a[0]) / d + 1 << endl;
	return 0;
}

试题I:后缀表达式
在这里插入图片描述
代码

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, m;
int a[N];
int main()
{
    cin >> n >> m;
    int k = n + m + 1;
    for (int i = 0; i < k; i++) {
        cin >> a[i];
    }
    sort(a, a + k);
    long long ans = 0;
    if (!m) {
        for (int i = 0; i < k; i++) {
            ans += a[i];
        }
    }
    else {
        ans += a[n + m] - a[0];

        for (int i = 1; i < k-1; i++) {
            ans += abs(a[i]);
        }

    }
    cout << ans << endl;
    return 0;
}

试题J:灵魂传输
在这里插入图片描述
代码:

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

const int N=3e5+10;

//由于a[]可能达到1e9所以需要使用到LL
typedef long long LL;

LL a[N];    //用于存放初始灵能值
LL s[N];    //用于存放前缀和
LL f[N];    //用于存放最终的有序序列

bool st[N];

int main()
{
    int T;
    scanf("%d",&T);

    while (T--)
    {
        int n;
        scanf("%d",&n);

        s[0]=0;// 注意这一步不要忘了

        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]),s[i]=s[i-1]+a[i];


        LL s0=s[0],sn=s[n];

        if(s0>sn)
            swap(s0,sn);

        sort(s,s+1+n);

        //找到排序后 s0,sn的位置
        for(int i=0;i<=n;i++)
            if(s0==s[i])
            {
                s0=i;
                break;
            }

        for(int i=0;i<=n;i++)
            if(sn==s[i])
            {
                sn=i;
                break;
            }

        memset(st,false,sizeof st);

        //构造重叠部分最小的序列

        int l=0,r=n;

        for(int i=s0;i>=0;i-=2)
            f[l++]=s[i],st[i]=true;

        for(int i=sn;i<=n;i+=2)
            f[r--]=s[i],st[i]=true;

        for(int i=0;i<=n;i++)
            if(st[i]==false)
                f[l++]=s[i];

        LL res=0;

        for(int i=1;i<=n;i++)
            res=max(res,abs(f[i]-f[i-1]));

        printf("%lld\n",res);

    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值