【2020年第十一届蓝桥杯C/C++ B组国赛】 个人题解

目录

 A: 美丽的 2

解题思路:

参考代码:

B: 扩散

解题思路:

参考代码:

C: 阶乘约数

解题思路:

参考代码:

D: 本质上升序列

解题思路:

参考代码:

E: 玩具蛇

解题思路:

参考代码:

F: 皮亚诺曲线距离

解题思路:

参考代码:

试题 G: 游园安排

解题思路:

参考代码:

试题 H: 答疑

解题思路:

参考代码:

 I: 出租车

解题思路:

参考代码:

J: 质数行者

解题思路:

参考代码:


 A: 美丽的 2

解题思路:

参考代码:

#include<bits/stdc++.h>  
using namespace std;  
const int N = 1e6 + 5;  
#define ll long long  
  
int main() {  
    int ans = 0;  
    for (int i = 1; i <= 2020; i++) {  
        int x = i;  
        while (x) {  
            if (x % 10 == 2) {  
                ans++;  
                break;  
            }  
            x /= 10;  
        }  
    }  
    cout << ans;  
    return 0;  
}  

B: 扩散

解题思路:

参考代码:

#include <bits/stdc++.h>  
using namespace std;  
const int N = 1e4 + 5;  
#define ll long long  
  
bool is[N][N];  
  
struct node {  
    int x, y, t;  
} s[5];  
int ans = 0;  
  
int dx[] = {0, 0, 1, -1};  
int dy[] = {1, -1, 0, 0};  
void bfs() {  
    queue<node>q;  
    for (int i = 1; i <= 4; i++) {  
        q.push({s[i].x, s[i].y, s[i].t});  
        is[s[i].x][s[i].y] = 1;  
    }  
    while (!q.empty()) {  
        node now = q.front();  
        q.pop();  
        for (int i = 0; i < 4; i++) {  
            int tx = now.x + dx[i];  
            int ty = now.y + dy[i];  
            if (tx >= 1 && ty >= 1 && tx < N && ty < N) {  
                if (is[tx][ty] == 0 && now.t + 1 <= 2020) {  
                    ans++;  
                    is[tx][ty] = 1;  
                    q.push({tx, ty, now.t + 1});  
                }  
            }  
        }  
    }  
}  
  
int main() {  
    int x = 3000;  
    s[1] = {0, 0, 0 }, s[2] = {2020, 11, 0 };  
    s[3] = {11, 14, 0 }, s[4] = {2000, 2000, 0 };  
    for (int i = 1; i <= 4; i++) {  
        s[i].x += x, s[i]. y += x;  
    }  
    bfs();  
    cout << ans + 4;  
    return 0;  
}  

C: 阶乘约数

解题思路:

唯一分解定理

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
int mp[N];
bool is[N];
vector<int>p(1);
ll ans = 1;
void init() {
	for (int i = 2; i <= 100; i++) {
		if (!is[i]) p.push_back(i);
		for (int j = 1; j < p.size() && i * p[j] <= 100; j++) {
			is[i * p[j]] = 1;
			if (i % p[j] == 0) break;
		}
	}
}
int main() {
	init();
	for (int i = 2; i <= 100; i++) {
		int x = i;
		while (x != 1) {
			for (int j = 1; j < p.size(); j++) {
				if (x % p[j] == 0) {
					mp[p[j]]++;
					x /= p[j];
					break;
				}
			}
		}
	}
	for (int i = 2; i <= 100; i++) {
		if (mp[i]) {
			ans *= mp[i] + 1;
		}
	}
	cout << ans;
	return 0;
}

D: 本质上升序列

解题思路:

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
string s = "?tocyjkdzcieoiodfpbgcncsrjbhmugdnojjddhllnofawllbhfiadgdcdjstemphmnjihecoapdjjrprrqnhgccevdarufmliqijgihhfgdcmxvicfauachlifhafpdccfseflcdgjncadfclvfmadvrnaaahahndsikzssoywakgnfjjaihtniptwoulxbaeqkqhfwl";
int n;
int f[205];
int ans;
int main() {
    n = s.size();
    for (int i = 1; i < n; i++) {
        f[i] = 1;
        for (int j = 1; j < i; j++) {
            if (s[i] > s[j]) f[i] += f[j];
            if (s[i] == s[j]) f[i] -= f[j];
        }
        ans += f[i];
    }
    cout << ans;
    return 0;
}

E: 玩具蛇

解题思路:

参考代码:

#include <bits/stdc++.h>  
using namespace std;  
const int N = 1e1 + 5;  
#define ll long long  
map<string, int>mp;  
bool is[N][N];  
int ans[N][N];  
  
int dx[] = {0, 0, 1, -1};  
int dy[] = {1, -1, 0, 0};  
void dfs(int x, int y, int k) {  
    if (k == 16) {  
        string s;  
        for (int i = 1; i <= 4; i++) {  
            for (int j = 1; j <= 4; j++) {  
                s.push_back(ans[i][j] + 'a');  
            }  
        }  
        mp[s]++;  
        return;  
    }  
    for (int i = 0; i < 4; i++) {  
        int tx = x + dx[i];  
        int ty = y + dy[i];  
        if (tx >= 1 && ty >= 1 && tx <= 4 && ty <= 4) {  
            if (is[tx][ty] == 0) {  
                ans[tx][ty] = k + 1;  
                is[tx][ty] = 1;  
                dfs(tx, ty, k + 1);  
                is[tx][ty] = 0;  
            }  
        }  
    }  
}  
int main() {  
    for (int i = 1; i <= 4; i++) {  
        for (int j = 1; j <= 4; j++) {  
            memset(is, 0, sizeof(is));  
            is[i][j] = 1;  
            ans[i][j] = 1;  
            dfs(i, j, 1);  
        }  
    }  
    cout << mp.size();  
    return 0;  
}  

试题F: 皮亚诺曲线距离

解题思路:

参考代码:

试题 G: 游园安排

解题思路:

参考代码:

试题 H: 答疑

解题思路:

贪心

参考代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
#define ll long long
int n;

struct node {
    ll s, a, e;
} t[N];
ll ans;

bool cmp(node a, node b) {
    return a.s + a.a + a.e < b.s + b.a + b.e;
}
ll sum[N];

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> t[i].s >> t[i].a >> t[i].e;
    }
    sort(t, t + n + 1, cmp);
    for (int i = 1; i <= n; i++) {
        sum[i] = t[i].s + t[i].a + t[i - 1].e+sum[i - 1];
        ans += sum[i];
    }
    cout << ans;
    return 0;
}

 I: 出租车

解题思路:

参考代码:

J: 质数行者

解题思路:

参考代码:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值