前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >第十一届蓝桥杯大赛软件类省赛B类第二场题解(C/C++)

第十一届蓝桥杯大赛软件类省赛B类第二场题解(C/C++)

作者头像
SakuraTears
发布2022-01-13 14:39:07
4150
发布2022-01-13 14:39:07
举报
文章被收录于专栏:从零开始的Code生活

编程题由于没法测试所以不知道对错,如有错误希望指正 填空题使用Python和C++来计算,有的地方用Python比C++方便些

A:门牌制作

答案:624 遍历转换为字符串然后继续遍历找2的个数

代码语言:javascript
复制
num = 0
for i in range(1, 2021):
    string = str(i)
    for j in range(0, len(string)):
        if (string[j] == '2'):
            num += 1

print(num)

B:既约分数

答案:2481215 辗转相除法

代码语言:javascript
复制
def gys(a, b):
    temp = a % b
    while (temp != 0):
        a = b
        b = temp
        temp = a % b
    return b

num = 0
for i in range(1, 2021):
    for j in range(1, 2021):
        if (gys(i, j) == 1):
            num += 1

print(num)

C:蛇形填数

答案:761 左下走:i + 1, j - 1 右上走:i - 1, j + 1

代码语言:javascript
复制
n = 2
i = 1
j = 2
jud = True
while (i != 20 or j != 20):
    if (i == 1 and jud == False):
        jud = True
        j += 1
        n += 1
        print(i, j, n)
    if (j == 1 and jud == True):
        i += 1
        jud = False
        n += 1
        print(i, j, n)
    if (jud == False):
        i -= 1
        j += 1
    else:
        j -= 1
        i += 1
    n += 1
    print(i, j, n)

D:跑步锻炼

答案:8879 没想到特别好的办法,if else解决了

代码语言:javascript
复制
day = 1
week = 6
month = 1
year = 2000
num = 0
while (True):
    if (week == 1 or day == 1):
        num += 2
    else:
        num += 1
    if (year == 2020 and month == 10 and day == 1):
        break
    if (month == 2):
        if (year % 4 == 0):
            if (day == 29):
                day = 0
                month = 3
        else:
            if (day == 28):
                day = 0
                month = 3
    elif (month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12):
        if (day == 31):
            day = 0
            if (month == 12):
                month = 1
                year += 1
            else:
                month += 1
    else:
        if (day == 30):
            day = 0
            month += 1
    day += 1
    week = (week + 1) % 7

print(num)

E:七段码

答案:80 并查集 + DFS

代码语言:javascript
复制
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;

const int N = 10;

int connect[N][N], num = 0, light[N], swi[N];

int find(int n)
{
    if (light[n] == n)
        return n;
    return find(light[n]);
}

void dfs(int n)
{
    if (n > 7) {
        for (int i = 0; i <= 7; i++)
            light[i] = i;
        for (int i = 0; i <= 7; i++) {
            for (int j = 0; j <= 7; j++) {
                if (swi[i] && swi[j] && connect[i][j]) {
                    int x = find(i);
                    int y = find(j);
                    if (x != y) {
                        light[x] = y;
                    }
                }
            }
        }
        int k = 0;
        for (int i = 0; i <= 7; i++) {
            if (swi[i] && light[i] == i)
                k++;
        }
        if (k == 1)
            num++;
        return;
    }
    swi[n] = 0;
    dfs(n + 1);
    swi[n] = 1;
    dfs(n + 1);
}

int main()
{
    connect[1][2] = connect[1][6] = 1;
    connect[2][1] = connect[2][7] = connect[2][3] = 1;
    connect[3][2] = connect[3][4] = connect[3][7] = 1;
    connect[4][3] = connect[4][5] = 1;
    connect[5][4] = connect[5][6] = connect[5][7] = 1;
    connect[6][1] = connect[6][5] = connect[6][7] = 1;
    dfs(1);
    cout << num;
    return 0;
}

F:成绩统计

这个不难,算是水题吧

代码语言:javascript
复制
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;

int main()
{
    int i = 0, n, m;
    float x1 = 0, x2 = 0;
    cin >> n;
    while (i++ < n)
    {
        cin >> m;
        if (m >= 60)
        {
            x1++;
            if (m >= 85)
            {
                x2++;
            }
        }
    }
    float a = x1 / n * 100, b = x2 / n * 100;
    float c = int(a) + 0.5, d = int(b) + 0.5;
    if (a >= c)
        a = int(a) + 1;
    else
        a = int(a);
    if (b >= d)
        b = int(b) + 1;
    else
        b = int(b);
    cout << a << "%" << endl
         << b << "%";
    return 0;
}

G:回文日期

输入n转换为字符串然后判断

代码语言:javascript
复制
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
#include <string>

using namespace std;

int main()
{
    int n;
    int res1 = INT_MAX, res2;
    cin >> n;
    string str;
    n++;
    while (true) {
        str = to_string(n);
        int i;
        for (i = 0; i < 4; i++) {
            if (str[i] != str[7 - i])
                break;
        }
        if (i == 4) {
            res1 = min(res1, n);
            if (str[0] == str[2] && str[1] == str[3]) {
                res2 = n;
                break;
            }
        }
        n++;
    }
    cout << res1 << endl
         << res2;
    return 0;
}

H:子串分值和

对每个子字符串区间用哈希表把出现的字符存起来,然后求map.size()。 需要注意的是在区间左边界向右移动时需要把map存的那个值 - 1,然后判断key == 0?。等于0应该把这个key去掉,不然求size会加上value为0的key

代码语言:javascript
复制
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main()
{
    map<char, int> mp;
    string str;
    int num = 0;
    cin >> str;
    for (int i = 0; i < str.size(); i++)
    {
        map<char, int> mp;
        for (int j = i; j < str.size(); j++)
        {
            mp[str[j]]++;
            num += mp.size();
        }
    }
    cout << num;
    return 0;
}

I、J

这俩不太会,过后再补

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年03月03日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • A:门牌制作
  • B:既约分数
  • C:蛇形填数
  • D:跑步锻炼
  • E:七段码
  • F:成绩统计
  • G:回文日期
  • H:子串分值和
  • I、J
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档