第十届蓝桥杯C++B组题解

在这里插入图片描述

A-组队

在这里插入图片描述
在这里插入图片描述
答案:490

/* -- A 组队 -- */
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
int a[20][5];
bool vis[20];
int res = 0;
void dfs(int u, int s)
{
    if(u == 5)
    {
        res = max(res, s);
        return ;
    }
    for(int i=0; i<20; i++)
    {
        if(!vis[i])
        {
            vis[i] = true;
            dfs(u + 1, s + a[i][u]);
            vis[i] = false;
        }
    }
}
int main() {
    for(int i=0; i<20; i++)
        for(int j=0; j<5; j++)
            cin >> a[i][j];
    dfs(0,0);
    cout << res << endl;
    return 0;
}

测试数据

97 95 0 0 0
92 85 96 0 0
0 0 0 0 93
0 0 0 80 86
89 83 97 0 0
82 86 0 0 0
0 0 0 87 90
0 97 96 0 0
0 0 89 0 0
95 99 0 0 0
0 0 96 97 0
0 0 0 93 98
94 91 0 0 0
0 83 87 0 0
0 0 98 97 98
0 0 0 93 86
98 83 99 98 81
93 87 92 96 98
0 0 0 89 92
0 99 96 95 81

B-年号子串

在这里插入图片描述
答案:BYQ

/* -- 年号子串 -- */
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;


int main() {
    string str = "ABCDEFGHIGKLMNOPQRSTUVWXYZ";
    int n;
    cin >> n;
    int t;
    char ans[10];
    int idx = 0;
    while(n)
    {
        int t = n % 26;
        n /= 26;
        ans[idx++] = str[t-1];
    }
    for(int i=idx-1; i>=0; i--)
        cout << ans[i];
    return 0;
}

C-数列求值

在这里插入图片描述
答案:4659

/* -- 数列求值 -- */

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

using namespace std;
const int N = 20190324;
const int M = 10000;
int a[N];
int main() {
    a[1] = a[2] = a[3] = 1;
    for(int i=4; i<=N; i++) a[i] = (a[i-1] % M + a[i-2] % M + a[i-3] % M) % M;
    cout << a[N] << endl; 
    return 0;
}

D-数的分解

在这里插入图片描述
答案:40785

/* -- 数的分解 -- */

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

using namespace std;

bool check(int n)
{
    while(n)
    {
        int t = n % 10;
        n /= 10;
        if(t == 2 || t == 4)
            return false;
    }
    return true;
}

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

E-迷宫

在这里插入图片描述
蓝桥杯大数据需要从文件读入。下面代码是刷题常用。

//包含的头文件
#include<fstream>
 
 
//从文件读入到数组
 ifstream fin("G:/test1.txt");   //   G:/test1.txt  为txt文档的绝对路径
 
for(int i=0;i<n;i++)
	fin>>a[i];
 
fin.close();   //关闭
 
//从数组输出到文件
ofstream fout("G:/test2.txt"); 
for(int i=0;i<n;i++)
	fout<<a[i];
 
fout.close();
/* -- 迷宫 -- */
#include <iostream>
#include <cstring>
#include <algorithm>
#include <fstream>
#include <queue>

using namespace std;

struct point{
    int x, y;
    string path;
    point(int x = 0, int y = 0, string path = ""): x(x), y(y),path(path){}
};

int dx[] = {1,0,0,-1}, dy[] = {0,-1,1,0};   //字典序最小
char dir[] = {'D','L','R','U'};
int g[32][52];
int vis[32][52];



void bfs(int n, int m)
{
    vis[1][1] = 1;
    queue<point> q;
    q.push({1,1,""});
    while(!q.empty())
    {
        auto cur = q.front();
        q.pop();
        if(cur.x == n && cur.y == m)
        {
            cout << cur.path << endl;
            return ;
        }
        for(int i=0; i<4; i++)
        {
            int a = cur.x + dx[i], b = cur.y + dy[i];

			if (a > 0 && b > 0 && a <= n && b <= m && !vis[a][b] && !g[a][b])
            {
                vis[a][b] = 1;
                q.push(point(a, b, cur.path + dir[i]));
            }
        }
    }
}

int main() {
    freopen("text.txt","r",stdin);
    char ch;
    for(int i=1; i<=30; i++)
        for(int j=1; j<=50; j++)
        {
        	cin >> ch;
			g[i][j] = ch - '0'; 
		}

    bfs(30,50);        

    return 0;
}

测试数据

01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000

F-特别数的和

在这里插入图片描述

/* -- 特别数的和 -- */

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

using namespace std;
typedef long long LL;
const int N = 10010;
int a[N];

bool check(int x)
{
    while(x)
    {
        int t = x % 10;
        x /= 10;
        if(t == 2 || t == 0 || t == 1 || t == 9)
            return true;
    }
    return false;
}

int main() {
    int n;
    scanf("%d",&n);
    LL res = 0;
    for(int i=1; i<=n; i++)
        if(check(i))
            res += i;

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

    return 0;
}

G-完全二叉树的权值

在这里插入图片描述

/* -- 完全二叉树的权值 -- */
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 100010;

typedef long long LL;
int a[N];
int n;

int main() {
    scanf("%d",&n);
    for(int i=1; i<=n; i++) scanf("%d",&a[i]);

    LL depth = 0, maxs = -100010;
    for(int d=1, i=1; i<=n; i*=2, d++)
    {
        LL s = 0;
        for(int j=i; j<i+(1<<d-1) && j<=n; j++)
            s += a[j];
        if(s > maxs)
        {
            maxs = s;
            depth = d;
        }
    }
    printf("%d\n",depth);

    return 0;
}

H-等差数列

在这里插入图片描述
在这里插入图片描述

/* -- 等差数列 -- */
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

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

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}
int main() {
    int n;
    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[0]);
    
    if(!d) printf("%d\n",n);
    else printf("%d\n",(a[n-1]-a[0])/d + 1);
    return 0;
}

I-后缀表达式

在这里插入图片描述

/* -- 后缀表达式 -- */
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long LL;
const int N = 200010;
int a[N];
int n, m;
int main() {
    scanf("%d%d",&n,&m);
    int k = n + m + 1;
    for(int i=0; i<k; i++) scanf("%d",&a[i]);

    LL res = 0;
    if(!m)
    {
        for(int i=0; i<k; i++)
            res += a[i];
    }
    else
    {
        sort(a, a+k);
        res = a[k-1] - a[0];
        for(int i=1; i<k-1; i++)
            res += abs(a[i]);
    }
    printf("%lld\n",res);
    return 0;
}

J-灵能传输

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

/* -- 灵能传输 -- */
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long LL;
const int N = 300010;
LL a[N], s[N];
bool st[N];

int main() {
    int T;
    scanf("%d", &T);
    while(T--)
    {
        memset(st, false, sizeof(st));
        int n;
        scanf("%d", &n);
        //1.将问题转换为前缀和
        for(int i=1; i<=n; i++) 
        {
            scanf("%lld",&a[i]);
            s[i] = s[i-1] + a[i];
        }
        
        //2.s0<sn,找出使差值最小的路径
        LL s0 = s[0], sn = s[n];
        if(s0 > sn) swap(s0,sn);    //指定s0<sn,交换对结果没有影响
        sort(s, s+n+1);

        for(int i=0; i<=n; i++)
            if(s[i] == s0)
            {
                s0 = i;
                break;
            }
        for(int i=0; i<=n; i++)
            if(s[i] == sn)
            {
                sn = i;
                break;
            }
        
        //3.路径的走法 -- 将结果存在a[]数组中
        int l = 0, r = n;
        for(int i=s0; i>=0; i-=2)
        {
            a[l++] = s[i];
            st[i] = true;
        }
        for(int i=sn; i<=n; i+=2)
        {
            a[r--] = s[i];
            st[i] = true;
        }
        for(int i=0; i<=n; i++)
            if(!st[i])
                a[l++] = s[i];
            
        LL res = 0;
        for(int i=1; i<=n; i++) res = max(res, abs(a[i]-a[i-1]));
        printf("%lld\n",res);
        
    }

    return 0;
}

QAQ电脑没电了!!!明天补一下大致思路!!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值