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

L1-1  人与神

解题思路:直接输出即可

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e3 + 5;
int main() {
	cout << "To iterate is human, to recurse divine.";
	return 0;
}

L1-2 两小时学完C语言

解题思路:小学算术

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e3 + 5;
int main() {
	int n, k, m;
	cin >> n >> k >> m;
	int ans = n - m * k;
	cout << ans;
	return 0;
}

L1-3 强迫症

解题思路:分类讨论

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e3 + 5;
int main() {
	string s;
	cin >> s;
	if (s.size() == 6) {
		cout << s.substr(0, 4) << '-' << s.substr(4, 2);
	} else {
		int x = stoi(s.substr(0, 2));
		if (x < 22) {
			cout << "20" << s.substr(0, 2) << '-' << s.substr(2, 2);
		} else cout << "19" << s.substr(0, 2)  << '-' << s.substr(2, 2);
	}
	return 0;
}

L1-4 降价提醒机器人

解题思路:比大小

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e3 + 5;
int main() {
	int n, m;
	cin >> n >> m;
	double x;
	while (n--) {
		cin >> x;
		if (x < m) {
			printf("On Sale! %.1lf\n", x);
		}
	}
	return 0;
}

L1-5 大笨钟的心情

解题思路:模拟即可

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e3 + 5;
int ha[25];
int main() {
	for (int i = 0 ; i < 24; i++) cin >> ha[i];
	int x;
	while (cin >> x) {
		if (x < 0 || x > 23) break;
		if (ha[x] > 50) {
			cout << ha[x] << ' ' << "Yes" << endl;
		} else {
			cout << ha[x] << ' ' << "No" << endl;
		}
	}
	return 0;
}

L1-6 吉老师的回归

解题思路:字符串的简单操作

参考代码:

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

int main() {
	int n, m;
	cin >> n >> m;
	m++;
	string s;
	vector<string>v;
	getchar();
	while (n--) {
		int t = 1;
		getline(cin, s);
		//cout << s << endl;
		for (int i = 0; i + 3  < s.size(); i++) {
			if (s.substr(i, 4) == "easy") {
				t = 0;
				break;
			}
		}
		for (int i = 0; i + 6 < s.size(); i++) {
			if (s.substr(i, 7) == "qiandao") {
				t = 0;
				break;
			}
		}
		if (t) v.push_back(s);
	}
	if (m > v.size()) cout << "Wo AK le";
	else cout << v[m - 1];
	return 0;
}

L1-7 天梯赛的善良

解题思路:计数统计即可

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e6 + 5;
int cnt[N];
int n;
int ma = 0, mi = 999999999;
int main() {
	cin >> n;
	int x;
	while (n--) {
		cin >> x;
		cnt[x]++;
		ma = max(ma, x);
		mi = min(mi, x);
	}
	cout << mi << ' ' << cnt[mi] << endl;
	cout << ma << ' ' << cnt[ma];
	return 0;
}

L1-8 乘法口诀数列

解题思路:模拟即可

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e3 + 5;
int a, b, n;
vector<int>ans;
int main() {
	cin >> a >> b >> n;
	ans.push_back(a);
	ans.push_back(b);
	int l = 0, r = 1;
	while (ans.size() < N) {
		int x = ans[l] * ans[r];
		string s = to_string(x);
		for (int i = 0; i < s.size(); i++) {
			ans.push_back(s[i] - '0');
		}
		l++, r++;
	}
	for (int i = 0; i < n - 1; i++) cout << ans[i] << ' ';
	cout << ans[n - 1];
	return 0;
}

L2-1   包装机

解题思路:小模拟

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e3 + 5;
deque<char>q[N];
stack<char>st;
deque<char>ans;
int main() {
	int n, m, ss;
	cin >> n >> m >> ss;
	string s;
	for (int i = 1; i <= n; i++) {
		cin >> s;
		for (int j = 0; j < m; j++) {
			q[i].push_back(s[j]);
		}
	}
	int x;
	while (cin >> x) {
		if (x == -1) break;
		if (x != 0) {
			if (st.size() == ss && !q[x].empty()) {
				ans.push_back(st.top());
				st.pop();
			}
			if (!q[x].empty()) {
				st.push(q[x].front());
				q[x].pop_front();
			}
		} else {
			if (!st.empty()) {
				ans.push_back(st.top());
				st.pop();
			}
		}
	}
	while (!ans.empty()) {
		cout << ans.front();
		ans.pop_front();
	}
	return 0;
}

L2-2 病毒溯源

解题思路:

参考代码:

L2-3 清点代码库

解题思路:

参考代码:

L2-4 哲哲打游戏

解题思路:模拟即可

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
int n, m, k;
vector<int>g[N];
int d[N];
int main() {
	cin >> n >> m;
	int x, y;
	for (int i = 1; i <= n; i++) {
		cin >> k;
		for (int j = 1; j <= k; j++) {
			cin >> x;
			g[i].push_back(x);
		}
	}
	int now = 1;
	for (int i = 1; i <= m; i++) {
		cin >> x >> y;
		if (x == 1) {
			d[y] = now;
			cout << now << endl;
		} else if (x == 2) {
			now = d[y];
		} else if (x == 0) {
			now = g[now][y - 1];
		}
	}
	cout << now;
	return 0;
}

L3-1

解题思路:

参考代码:

L3-2 还原文件

解题思路:DFS+回溯

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
vector<int>g[N], ans;
int len, n, k, x;
int a[N];
bool is[N];
int ind = 1, res;
void dfs() {
	if (n == ans.size()) {
		for (int i = 0; i < ans.size() - 1; i++) cout << ans[i] << ' ';
		cout << ans.back();
		exit(0);
	}
	for (int i = 1; i <= n; i++) {
		if (is[i] == 0) {
			int ok = 1;
			res = ind;
			int in = ind;
			if (a[ind] == g[i].front()) {
				for (int j = 0; j < g[i].size(); j++) {
					if (g[i][j] != a[res]) {
						ok = 0;
						break;
					}
					res++;
				}
			} else ok = 0;
			if (ok) {
				is[i] = 1;
				ans.push_back(i);
				ind = res - 1;
				dfs();
				is[i] = 0;
				ans.pop_back();
				ind = in;
			}
		}
	}
}
int main() {
	cin >> len;
	for (int i = 1; i <= len; i++) cin >> a[i];
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> k;
		for (int j = 1; j <= k; j++) {
			cin >> x;
			g[i].push_back(x);
		}
	}
	dfs();
	return 0;
}

L3-3

解题思路:

参考代码:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值