2025寒假天梯赛训练1
2025寒假天梯赛训练1
7-1 心理阴影面积
思路
用一半的面积减去一个梯形和一个三角形面积即可。
代码
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int a, b; cin >> a >> b; cout << (5000 - (100 - a) * 50 - b * 50) << "\n"; return 0; }
7-2 人与神
思路
直接输出即可。
代码
To iterate is human, to recurse divine.
7-3 通讯录的录入与显示
思路
按题意输出即可。
注意记录编号可能为负数。
代码
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<array<string,5>> s(n); for(auto &x : s){ for(int i = 0;i < 5;i ++){ cin >> x[i]; } } int k; cin >> k; while(k--){ int x; cin >> x; if(x >= n || x < 0){ cout << "Not Found\n"; }else{ cout << s[x][0] << " " << s[x][3] << " " << s[x][4] << " " << s[x][2] << " " << s[x][1] << "\n"; } } return 0; }
7-4 算术入门之加减乘除
思路
按题意要求输出即可。
代码
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int a,b; cin >> a >> b; cout << a << " + " << b << " = " << a + b << "\n" ; cout << a << " - " << b << " = " << a - b << "\n" ; cout << a << " * " << b << " = " << a * b << "\n" ; if(a % b == 0){ cout << a << " / " << b << " = " << a / b << "\n" ; }else{ cout << fixed << setprecision(2) << a << " / " << b << " = " << a * 1.0 / b << "\n" ; } return 0; }
7-5 出生年
思路
按题意模拟。
代码
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int a,b; cin >> a >> b; int cs = a; while(1){ set<int> has; if(a < 1000){ has.insert(0); } int t = a; while(t){ has.insert(t%10); t/=10; } if(has.size() == b){ printf("%d %04d\n",a-cs,a); break; } a ++; } return 0; }
7-6 九宫格输入法
思路
按题意模拟。
代码
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); vector<string> s{ "0 ", "1,.?!", "2ABC", "3DEF", "4GHI", "5JKL", "6MNO", "7PQRS", "8TUV", "9WXYZ" }; string x; while (cin >> x) { int a = x[0] - '0'; int b = x.size(); b--; b %= (int)s[a].size(); cout << s[a][b]; } return 0; }
7-7 螺旋方阵
思路
按题意模拟。
代码
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector a(n + 1, vector(n + 1, 0)); int has = 0, x = 1, y = 0; while (has < n * n) { while(y + 1 <= n && !a[x][y + 1]){ y ++; a[x][y] = ++has; } while(x + 1 <= n && !a[x + 1][y]){ x ++; a[x][y] = ++has; } while(y - 1 >= 1 && !a[x][y - 1]){ y --; a[x][y] = ++has; } while(x - 1 >= 1 && !a[x-1][y]){ x --; a[x][y] = ++has; } } for(int i = 1;i <= n;i ++){ for(int j = 1;j <= n;j ++){ cout << setw(3) << a[i][j]; } cout << "\n"; } return 0; }
7-8 抓老鼠啊~亏了还是赚了?
思路
有点需要注意细节的模拟。
代码
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string s; cin >> s; int happy = 0, nohappy = 0, sad = 0; int nai = 0, now = 0; string ans = ""; auto clear = [&]() { happy--; happy = max(happy, 0); nohappy--; nohappy = max(nohappy, 0); sad--; sad = max(sad, 0); }; for (int i = 0; i < s.size() - 1; i ++) { if (s[i] == 'X') { if (happy || (!nohappy && !sad)) { ans += "U"; nohappy = 2; } else { ans += "-"; } } else if (s[i] == 'T') { if (happy || (!nohappy && !sad)) { ans += "D"; now += 7; sad = 3; } else { ans += "-"; } } else { if (happy || (!nohappy && !sad)) { ans += "!"; now -= 3; happy = 3; } else { ans += "-"; } } clear(); } cout << ans << "\n" << now << "\n" ; return 0; }
7-9 Windows消息队列
思路
用优先队列维护优先级即可。
代码
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; priority_queue<pair<int, string>,vector<pair<int, string>>,greater<pair<int, string>>> Q; while (n--) { string x; cin >> x; if (x == "GET") { if (Q.empty()) { cout << "EMPTY QUEUE!\n"; } else { auto [_, s] = Q.top(); Q.pop(); cout << s << "\n"; } } else { int m; cin >> x >> m; Q.emplace(m, x); } } return 0; }
7-10 名人堂与代金券
思路
考察排序。
代码
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n,g,k; cin >> n >> g >> k; vector<pair<string,int>> a(n); int need = 0; for(auto &[x,y] : a){ cin >> x >> y; if(y >= g){ need += 50; }else if(y >= 60){ need += 20; } } cout << need << "\n"; sort(a.begin(),a.end(),[](auto x,auto y){ if(x.second != y.second) return x.second > y.second; return x.first < y.first; }); int rank = 1; for(int i = 0;i < n;i ++){ if(rank > k) break; cout << rank << " " << a[i].first << " " << a[i].second << "\n"; while(i + 1 < n && a[i + 1].second == a[i].second){ i ++; cout << rank << " " << a[i].first << " " << a[i].second << "\n"; } rank = i + 2; } return 0; }
7-11 用扑克牌计算24点
思路
答辩题。
实际要注意的细节比较多,括号的顺序可以直接列出来然后挨个判断,其余的就是暴力搜索了。
代码
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string a[4]; for (int i = 0; i < 4; i ++) { cin >> a[i]; } auto check = [&](string & s)->bool{ int i = 0; auto calc = [](double x, double y, char op)->double{ if (op == '+') return x + y; if (op == '-') return x - y; if (op == '*') return x * y; return x * 1.0 / y; }; auto Calc = [&](auto && self)->double{ char op = '+'; stack<double> st; for (; i < s.size(); i++) { if (s[i] == '(') { i ++; st.push(self(self)); } else if (s[i] != ')') { if (isdigit(s[i])) { int x = s[i] - '0'; if (isdigit(s[i + 1])) { x = x * 10 + s[i + 1] - '0'; i ++; } st.push(x); } else { op = s[i]; } } else { double y = st.top(); st.pop(); double x = st.top(); st.pop(); return calc(x, y, op); } } if (st.size() > 1) { double y = st.top(); st.pop(); double x = st.top(); st.pop(); st.push(calc(x, y, op)); } return st.top(); }; bool ok = Calc(Calc) == 24; return ok; }; string p = "+-*/"; string s = "((2-(3+12))+12)"; sort(a, a + 4); do { for (auto x : p) { for (auto y : p) { for (auto z : p) { s = "((" + a[0] + x + a[1] + ")" + y + a[2] + ")" + z + a[3]; if (check(s)) { cout << s << "\n"; return 0; } s = "(" + a[0] + x + a[1] + ")" + y + "(" + a[2] + z + a[3] + ")"; if (check(s)) { cout << s << "\n"; return 0; } s = a[0] + x + "(" + a[1] + y + "(" + a[2] + z + a[3] + "))" ; if (check(s)) { cout << s << "\n"; return 0; } s = a[0] + x + "((" + a[1] + y + a[2] + ")" + z + a[3] + ")"; if (check(s)) { cout << s << "\n"; return 0; } s = "(" + a[0] + x + "(" + a[1] + y + a[2] + "))" + z + a[3]; if (check(s)) { cout << s << "\n"; return 0; } } } } } while (next_permutation(a, a + 4)); cout << "-1\n"; return 0; }
7-12 玩转二叉树
思路
先用中序前序按层还原二叉树,然后对于每一层反向输出即可。
代码
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> pre(n), mid(n); for (int i = 0; i < n; i ++) { cin >> mid[i]; } for (int i = 0; i < n; i ++) { cin >> pre[i]; } map<int, vector<int>> tr; int len = 0; auto dfs = [&](auto && self, int rt, int st, int ed, int len)->void{ if (st > ed) { return; } int root = st; while (root < ed && pre[rt] != mid[root]) { root ++; } self(self, rt + 1, st, root - 1, len + 1); self(self, rt + root - st + 1, root + 1, ed, len + 1); tr[len].emplace_back(pre[rt]); }; dfs(dfs, 0, 0, n - 1, 0); vector<int> ans; for (int i = 0; i < n; i ++) { if (tr[i].size()) { for (int j = tr[i].size() - 1; j >= 0; j --) { ans.push_back(tr[i][j]); } } } for (int i = 0; i < n; i ++) { cout << ans[i] << " \n"[i == n - 1]; } return 0; }
本文作者:Ke_scholar
本文链接:https://www.cnblogs.com/Kescholar/p/18691586
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步