【2023第八届团体程序设计天梯赛】个人题解(C++)

大一第一次参加,混了191分,差一点上200分😭

 

目录

L1-1 最好的文档

解题思路

参考代码

L1-2  什么是机器学习

解题思路

参考代码

L1-3 程序员买包子

解题思路

参考代码

L1-4  进化论

解题思路

参考代码

L1-5 猜帽子游戏

解题思路

参考代码

L1-6  剪切粘贴

解题思路

参考代码

L1-7 分寝室

解题思路

参考代码

L1-8  谁管谁叫爹

解题思路

参考代码

L2-1 堆宝塔

解题思路

参考代码

L2-2 天梯赛的赛场安排

解题思路

参考代码

L2-3 锦标赛

解题思路

参考代码

L2-4  寻宝图

解题思路

参考代码


L1-1 最好的文档

解题思路

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
int main() {
	cout << "Good code is its own best documentation.";
	return 0;
}

L1-2  什么是机器学习

解题思路

小学算术

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
int a, b;
int main() {
	cin >> a >> b;
	int ans = a + b;
	cout << ans - 16 << endl;
	cout << ans - 3 << endl;
	cout << ans - 1 << endl;
	cout << ans  << endl;
	return 0;
}

L1-3 程序员买包子

解题思路

简单分类讨论

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
int n, m, k;
string s;
int main() {
	cin >> n >> s >> m >> k;
	if (k == n) {
		cout << "mei you mai " << s << " de";
		return 0;
	}
	if (k == m) {
		cout << "kan dao le mai " << s << " de";
		return 0;
	}
	cout << "wang le zhao mai " << s << " de";
	return 0;
}

L1-4  进化论

解题思路

简单分类讨论

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
int n, a, b, c;
int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a >> b >> c;
		if (c == a * b) {
			cout << "Lv Yan" << endl;
			continue;
		}
		if (c == a + b) {
			cout << "Tu Dou" << endl;
			continue;
		}
		cout << "zhe du shi sha ya!" << endl;
	}
	return 0;
}

L1-5 猜帽子游戏

解题思路

按题目要求比就行了

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
int n, k;
int a[N], b[N];
int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	cin >> k;
	while (k--) {
		for (int i = 1; i <= n; i++) cin >> b[i];
		int ok1 = 0, ok2 = 0;
		int n0 = 0;
		for (int i = 1; i <= n; i++) {
			if (b[i] == 0) n0++;
			else if (a[i] == b[i]) ok1 = 1;
			else ok2 = 1;
		}
		if (n0 == n) {
			cout << "Ai Ya" << endl;
			continue;
		}
		if (ok2 == 1) {
			cout << "Ai Ya" << endl;
			continue;
		}
		if (ok1 == 1) {
			cout << "Da Jiang!!!" << endl;
			continue;
		}
	}
	return 0;
}

L1-6  剪切粘贴

解题思路

善用string库函数就行了

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
string s;
int n;
string a, b;
int l, r;
int main() {
	cin >> s >> n;
	while (n--) {
		cin >> l >> r >> a >> b;
		string x = s.substr(l - 1, r - l + 1);
		s.erase(l - 1, r - l + 1);
		string y = a + b;
		if (s.find(y) == -1) {
			s += x;
			continue;
		}
		int pos = s.find(y);
		s.insert(pos + a.size(), x);
	}
	cout << s;
	return 0;
}

L1-7 分寝室

解题思路

枚举n0,那么n1也就出来了,然后就是更新答案即可

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
int n, n0, n1;
int main() {
	cin >> n0 >> n1 >> n;
	int x, y;
	int a, b;
	int ans = 999999;
	for (int i = 2; i <= n0; i++) {
		if (n0 % i != 0) continue;
		a = n0 / i;
		b = n - a;
		if (n1 % b != 0) continue;
		if (abs(a - b) < ans) {
			ans = abs(a - b);
			x = a, y = b;
		}
	}
	if (ans == 999999) cout << "No Solution";
	else cout << x << ' ' << y;
	return 0;
}

L1-8  谁管谁叫爹

解题思路

分类讨论

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
int sum(ll x) {
	int s = 0;
	while (x) {
		s += x % 10;
		x /= 10;
	}
	return s;
}
ll n, a, b;
int s1, s2;
int main() {
	cin >> n;
	while (n--) {
		cin >> a >> b;
		s1 = sum(a), s2 = sum(b);
		int is1 = 0, is2 = 0;
		if (a % s2 == 0) is1 = 1;
		if (b % s1 == 0) is2 = 1;
		if ((is1 == 1 && is2 == 1)) {
			if (a > b) {
				cout << "A" << endl;
				continue;
			} else {
				cout << "B" << endl;
				continue;
			}
		}
		if ((is1 == 0 && is2 == 0)) {
			if (a > b) {
				cout << "A" << endl;
				continue;
			} else {
				cout << "B" << endl;
				continue;
			}
		}
		if (is1 == 1) {
			cout << "A" << endl;
			continue;
		}
		if (is2 == 1) {
			cout << "B" << endl;
			continue;
		}
	}
	return 0;
}

L2-1 堆宝塔

解题思路

一眼模拟

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
int n;
int ma, cnt;
stack<int>a, b;
int c;
int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> c;
		if (a.empty() || c < a.top()) {
			a.push(c);
			ma = max(ma, (int)a.size());
		} else {
			if (b.empty() || c > b.top()) {
				b.push(c);
			} else {
				cnt++;
				while (!a.empty()) a.pop();
				while (!b.empty() && b.top() > c) {
					a.push(b.top());
					b.pop();
				}
				a.push(c);
				ma = max(ma, (int)a.size());
			}
		}
	}
	if (!a.empty()) cnt++;
	if (!b.empty()) cnt++;
	cout << cnt << ' ' << ma;
	return 0;
}

L2-2 天梯赛的赛场安排

解题思路

题目提到每次操作取最大值,直接上优先队列模拟即可

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 5e3 + 5;
struct node {
	string s;
	int m;
	int y;
	int id;
	bool friend operator < (node a, node b) {
		return a.m < b.m;
	}
} a[N];
int n, c;
vector<int>v;
priority_queue<node>q;
int cnt;
int main() {
	cin >> n >> c;
	for (int i = 1; i <= n; i++) {
		cin >> a[i].s >> a[i].m;
		a[i].id = i;
		q.push(a[i]);
	}
	while (!q.empty()) {
		node now = q.top();
		q.pop();
		if (now.m >= c) {
			a[now.id].y += now.m / c;
			cnt += now.m / c;
			now.m %= c;
			if (now.m)q.push(now);
		} else {
			if (v.empty()) {
				cnt++;
				a[now.id].y++;
				v.push_back(now.m);
			} else {
				int is = 1;
				for (int i = 0; i < v.size(); i++) {
					if (v[i] + now.m <= c) {
						is = 0;
						a[now.id].y++;
						v[i] += now.m;
						break;
					}
				}
				if (is) {
					cnt++;
					a[now.id].y++;
					v.push_back(now.m);
				}
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		cout << a[i].s << ' ' << a[i].y << endl;
	}
	cout << cnt;
	return 0;
}

L2-3 锦标赛

解题思路

16分代码,思路就是一个一个比,把大的填进去

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
int k;
int a[20][N];
int main() {
	cin >> k;
	int tot = 0;
	for (int i = 1; i <= k; i++) {
		tot = 1;
		for (int j = 1; j <= pow(2, k - i); j++) {
			cin >> a[i][tot];
			tot += 2;
		}
	}
	cin >> a[k + 1][1];
	a[k][2] = a[k + 1][1];
	bool is[N];
	vector<int>v;
	v.push_back(a[k][1]);
	v.push_back(a[k][2]);
	for (int i = k - 1; i >= 1; i--) {
		tot = 2;
		memset(is, 0, sizeof(is));
		for (int j = 1; j <= pow(2, k - i); j++) {
			int ok = 1;
			for (int k = 0; k < v.size(); k++) {
				if (is[k] == 0 && v[k] >= a[i][tot - 1]) {
					is[k] = 1;
					a[i][tot] = v[k];
					ok = 0;
					break;
				}
			}
			if (ok) {
				cout << "No Solution";
				return 0;
			}
			tot += 2;
		}
		v.clear();
		for (int j = 1; j <= tot - 2; j++) {
			v.push_back(a[i][j]);
		}
	}
	for (int i = 1; i < pow(2, k); i++) cout << a[1][i] << " ";
	cout << a[1][(int)pow(2, k)];
	return 0;
}

L2-4  寻宝图

解题思路

简简单单连通块,直接搜就行了

参考代码

#include<bits/stdc++.h>
using namespace std;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int n, m;
int cnt1, cnt2;
struct point {
	int x, y;
};
int is;
int main() {
	cin >> n >> m;
	char mp[n + 1][m + 1];
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> mp[i][j];
		}
	}
	queue<point>q;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (mp[i][j] != '0') {
				is = 0;
				cnt1++;
				q.push({i, j});
				if (mp[i][j] > '1') is = 1;
				mp[i][j] = '0';
				while (!q.empty()) {
					point now = q.front();
					q.pop();
					for (int k = 0; k < 4; k++) {
						int tx = now.x + dx[k];
						int ty = now.y + dy[k];
						if (tx >= 1 && ty >= 1 && tx <= n && ty <= m) {
							if (mp[tx][ty] != '0') {
								q.push({tx, ty});
								if (mp[tx][ty] > '1') is = 1;
								mp[tx][ty] = '0';
							}
						}
					}
				}
				if (is) cnt2++;
			}
		}
	}
	cout << cnt1 << ' ' << cnt2;
	return 0;
}

L3考试没时间写😭

  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值