2020年蓝桥杯省赛第二场C++B组题解

2021/7/16 12:35:41

本文主要是介绍2020年蓝桥杯省赛第二场C++B组题解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

A.

暴力计算即可。

// #pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO                       \
    ios::sync_with_stdio(false); \
    // cout.tie(0);
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 2e8 + 10;
const int maxm = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);
int dis[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int ans = 0;
int count(int n)
{
    int cnt = 0;
    while (n)
    {
        int t = n % 10;
        if (t == 2)
            cnt++;
        n = n / 10;
    }
    return cnt;
}
int main()
{
#ifdef WXY
    freopen("in.txt", "r", stdin);
//	 freopen("out.txt", "w", stdout);
#endif
    IO;
    for (int i = 1; i <= 2020; i++)
        ans += count(i);
    cout << ans;
    return 0;
}

624

容易找规律发现相邻两项之间的关系。

// #pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO                       \
    ios::sync_with_stdio(false); \
    // cout.tie(0);
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 2e8 + 10;
const int maxm = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);
int dis[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main()
{
#ifdef WXY
    freopen("in.txt", "r", stdin);
//	 freopen("out.txt", "w", stdout);
#endif
    IO;
    int a[50];
    int t = 1;
    a[1] = 1;
    for (int i = 2; i <= 20;i++)
        a[i] = a[i - 1] + t * 4, t++;
    cout << a[20];
    return 0;
}

761

C.

暴力判断即可。

// #pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO                       \
    ios::sync_with_stdio(false); \
    // cout.tie(0);
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 2e8 + 10;
const int maxm = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);
int dis[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
int main()
{
#ifdef WXY
    freopen("in.txt", "r", stdin);
//	 freopen("out.txt", "w", stdout);
#endif
    IO;
    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;
    return 0;
}

2481215

D.

比赛的时候因为怕数迷,就没用日历表数,现在回想一下真该检查的时候数一遍。

这题我写的8880,就是因为打印函数返回值出的锅(调用函数会导致全局变量改变),但凡当时用变量存一下函数返回值也不会错了,一念之间。。。省一没了。。。。

// #pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO                       \
	ios::sync_with_stdio(false); \
	// cout.tie(0);
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 2e8 + 10;
const int maxm = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);
int dis[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int w = 5;
bool rui(int n)
{
	if (n % 400 == 0 || (n % 4 == 0 && n % 100 != 0))
		return true;
	return false;
}
int count(int year)
{
	int cnt = 0;
	if (rui(year))
		m[2] += 1;
	if (year == 2020)
	{
		for (int i = 1; i <= 9; i++)
		{
			for (int j = 1; j <= m[i]; j++)
			{
				if (w == 0 || j == 1)
				{
					cnt++;
				}
				w = (w + 1) % 7;
			}
		}
	}
	else
	{
		for (int i = 1; i <= 12; i++)
		{
			for (int j = 1; j <= m[i]; j++)
			{
				if (w == 0 || j == 1)
				{
					cnt++;
				}
				w = (w + 1) % 7;
			}
		}
	}
	if (rui(year))
		m[2] -= 1;
	return cnt;
}
int main()
{
#ifdef WXY
	freopen("in.txt", "r", stdin);
//	 freopen("out.txt", "w", stdout);
#endif
	IO;
	int ans = 0;
	for (int i = 2000; i <= 2020; i++)
	{
		// cout << count(i) << endl;
		// 一念之间 天堂地狱
		// 闲的没事非打印干嘛?!
		// 打印完了还不注释掉?!
		ans += count(i);
	}
	cout << ans + 7580 + 1; // 最后再加上10.1这一天
	return 0;
}

8879

E.

可以用二维数组建图然后跑bfs判断是否连通。

#include<bits/stdc++.h>
using namespace std;
int dis[6][2] = {1, -1, -1, 1, -1, -1, 1, 1, 2, 0, -2, 0};
struct Node
{
    int x, y;
    Node(){};
    Node(int a, int b)
    {
        x = a, y = b;
    }
};
int g[10][10];
bool book[10][10];
bool vis[10];
int a[10];
int C, ans;
bool BFS(int all)
{
    memset(book, false, sizeof book);
    int sx, sy;
    for (int i = 1; i <= 5; i++)
        for (int j = 1; j <= 3; j++)
            if (g[i][j] == 1)
            {
                sx = i, sy = j;
                break;
            }

    queue<Node> q;
    q.push(Node(sx, sy));
    book[sx][sy] = true;
    int cnt = 0;
    while (!q.empty())
    {
        Node now = q.front();
        q.pop();
        cnt++;
        if (now.y == 2)
        {
            for (int i = 0; i < 4; i++)
            {
                int tx = now.x + dis[i][0];
                int ty = now.y + dis[i][1];
                if (tx >= 1 && tx <= 5 && ty >= 1 && ty <= 3 && g[tx][ty] == 1 && book[tx][ty] == false)
                {
                    book[tx][ty] = true;
                    q.push(Node(tx, ty));
                }
            }
        }
        else
        {
            for (int i = 0; i < 6; i++)
            {
                int tx = now.x + dis[i][0];
                int ty = now.y + dis[i][1];
                if (tx >= 1 && tx <= 5 && ty >= 1 && ty <= 3 && g[tx][ty] == 1 && book[tx][ty] == false)
                {
                    book[tx][ty] = true;
                    q.push(Node(tx, ty));
                }
            }
        }
    }
    return cnt == all;
}
void draw(int val)
{
    if (val == 1)
        g[1][2] = 1;
    if (val == 2)
        g[2][3] = 1;
    if (val == 3)
        g[4][3] = 1;
    if (val == 4)
        g[5][2] = 1;
    if (val == 5)
        g[4][1] = 1;
    if (val == 6)
        g[2][1] = 1;
    if (val == 7)
        g[3][2] = 1;
    return;
}
void DFS(int now, int sum)
{
    if (sum == C)
    {
        memset(g, 0, sizeof g);
        for (int i = 0; i < sum; i++)
            draw(a[i]);
        if (BFS(sum))
            ans++;
        return;
    }
    for (int i = now; i <= 7; i++)
    {
        if (vis[i] == false)
        {
            vis[i] = true;
            a[sum] = i;
            DFS(i + 1, sum + 1);
            vis[i] = false;
        }
    }
    return;
}
int main()
{

    for (int i = 1; i <= 7; i++)
    {
        C = i;
        DFS(1, 0);
    }
    cout << ans;
    return 0;
}

80

F

// #pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO                       \
    ios::sync_with_stdio(false); \
    // cout.tie(0);
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 2e8 + 10;
const int maxm = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);
int dis[6][2] = {1, -1, -1, 1, -1, -1, 1, 1, 2, 0, -2, 0};
int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main()
{
#ifdef WXY
    freopen("in.txt", "r", stdin);
//	 freopen("out.txt", "w", stdout);
#endif
    IO;
    int n, x;
    int cnt1, cnt2 = 0;
    char c = '%';
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> x;
        if (x >= 60)
            cnt1++;
        if (x >= 85)
            cnt2++;
    }
    printf("%.0lf", round(100.0 * double(cnt1) / double(n)));
    printf("%c\n", c);
    printf("%.0lf", round(100.0 * double(cnt2) / double(n)));
    printf("%c", c);
    return 0;
}

G

当时抽疯写了个200+的弱智暴力(还不一定能对),其实只要枚举年份构造出回文串,然后把符合条件的日期存起来,二分答案即可。

H

太菜了只会暴力做,O(n^3)

是牛客多校上一道原题。

统计每个字符,对每个字符记录它上一次出现的位置vis,当前字符所能贡献的区间个数为(i-pre)*(n-i+1),i为当前字符下标。

// #pragma GCC optimize(2)
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <random>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#define IO                       \
	ios::sync_with_stdio(false); \
	// cout.tie(0);
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1e5 + 10;
const int maxm = 1e6 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);
int dis[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
// int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
LL vis[maxn];
char s[maxn];
int main()
{
#ifdef WXY
	freopen("in.txt", "r", stdin);
//	 freopen("out.txt", "w", stdout);
#endif
	scanf("%s", s + 1);
	int n = strlen(s + 1);
	LL ans = 0;
	for (int i = 1; i <= n; i++)
	{
		int t = s[i] - 'a' + 1;
		ans = ans + LL(i - vis[t]) * LL(n - i + 1);
		vis[t] = i;
	}
	cout << ans;
	return 0;
}

I

输出样例

当时如果仔细想想,应该能把n<=4的情况暴力模拟出来。。

J

输出样例

不出意外,省一应该没了,还是太菜了。。。。

2020.10.27更新

居然省一了,本来以为填空全对才能摸到省一,没想到蓝桥杯这么水。。。



这篇关于2020年蓝桥杯省赛第二场C++B组题解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程