2024年6月份电子学会青少年软件编程(C/C++语言)等级考试试卷(七级)分享

文摘   2024-09-06 16:26   安徽  

青少年软件编程(C语言)等级考试试卷(级)2024-6

2406】C      分数:100分题数:4

1.玩转二叉树

 

#include<bits/stdc++.h>using namespace std;const int maxn = 505;bool lost[maxn], vst[maxn];vector<int> G[maxn];int n, m, ans;void dfs(int u){vst[u] = true;for(int & x : G[u]) if(!lost[x] && !vst[x]) dfs(x);}int test(){int res = 0;for(int i = 0; i < n; i++) vst[i] = false;for(int i = 0; i < n; i++){if(lost[i]) continue;if(!vst[i]) ++res, dfs(i);}return res;}int main(){#ifdef MyTestfreopen("Sakura.txt", "r", stdin);#endifcin >> n >> m;for(int i = 0; i < m; i++){int x, y;cin >> x >> y;G[x].push_back(y);G[y].push_back(x);}int k;cin >> k;ans = test();for(int i = 0; i < k; i++){int x;cin >> x;lost[x] = true;int now = test();if(now > ans) printf("Red Alert: City %d is lost!\n", x);else printf("City %d is lost.\n", x);ans = now;}if(k == n) puts("Game Over.");return 0;}

2.红色警报

 

#include<bits/stdc++.h>using namespace std;const int maxn = 505;bool lost[maxn], vst[maxn];vector<int> G[maxn];int n, m, ans;void dfs(int u){vst[u] = true;for(int & x : G[u]) if(!lost[x] && !vst[x]) dfs(x);}int test(){int res = 0;for(int i = 0; i < n; i++) vst[i] = false;for(int i = 0; i < n; i++){if(lost[i]) continue;if(!vst[i]) ++res, dfs(i);}return res;}int main(){#ifdef MyTestfreopen("Sakura.txt", "r", stdin);#endifcin >> n >> m;for(int i = 0; i < m; i++){int x, y;cin >> x >> y;G[x].push_back(y);G[y].push_back(x);}int k;cin >> k;ans = test();for(int i = 0; i < k; i++){int x;cin >> x;lost[x] = true;int now = test();if(now > ans) printf("Red Alert: City %d is lost!\n", x);else printf("City %d is lost.\n", x);ans = now;}if(k == n) puts("Game Over.");return 0;}

3.到底爱不爱我

 

#include <iostream>#include <vector>#include <string>using namespace std;
// 定义情枝的类型enum BranchType { Exclusive = 1, // 专情枝 Inclusive, // 博爱枝 Flip // 情变枝};
// 情枝结构struct Branch { BranchType type; int left; // 左分枝连接的编号 int right; // 右分枝连接的编号};
// 判断情枝的输出char judgeBranch(const Branch &b, vector<bool> &endStates) { bool leftState = (b.left == 0) ? false : endStates[b.left]; bool rightState = (b.right == 0) ? false : endStates[b.right]; switch (b.type) { case Exclusive: return leftState && rightState ? 'A' : 'B'; case Inclusive: return leftState && rightState ? 'B' : 'A'; case Flip: return leftState ? 'B' : 'A'; } return 'B'; // 默认返回 BuAi}
int main() { int N, K; cin >> N; vector<Branch> branches(N); for (int i = 0; i < N; ++i) { int type, left, right; cin >> type >> left >> right; branches[i] = {type == 1 ? Exclusive : (type == 2 ? Inclusive : Flip), left - 1, right - 1}; } cin >> K; vector<bool> endStates(N, false); // 初始化所有末端状态为 false for (int i = 0; i < K; ++i) { string input; cin >> input; for (int j = 0; j < N; ++j) { // 设置末端状态 if (branches[j].left == 0 && branches[j].right == 0) { endStates[j] = input[j] == '1'; } } // 从末端向根节点递归判断 for (int j = 0; j < N; ++j) { if (branches[j].left != 0 || branches[j].right != 0) { endStates[j] = judgeBranch(branches[j], endStates) == 'A'; } } // 输出根节点的结果 if (endStates[0]) { cout << "Ai" << endl; } else { cout << "BuAi" << endl; } // 重置末端状态 fill(endStates.begin(), endStates.end(), false); } return 0;}

4.兰州拉面派餐系统

 

#include <iostream>#include <vector>#include <queue>#include <map>using namespace std;
// 定义篮子结构struct Basket { bool isFree = true; // 篮子是否空闲 int noodleType = 0; // 篮子中煮的面的种类 int endTime = 0; // 篮子中面条的完成时间};
int main() { int N, M, L; cin >> N >> M >> L; vector<int> noodleTimes(N); for (int i = 0; i < N; ++i) { cin >> noodleTimes[i]; } vector<int> orders(L); for (int i = 0; i < L; ++i) { cin >> orders[i]; }
vector<Basket> baskets(M); vector<pair<int, int>> orderTimes; // 存储订单和送餐时间 map<int, int> basketCounts; // 存储每个篮子煮了多少碗面
int currentTime = 0; // 当前时间 for (int i = 0; i < L; ++i) { int order = orders[i] - 1; // 客单所点的面种 int minBasket = -1; // 找到空闲的篮子,或者编号最小的篮子 for (int j = 0; j < M; ++j) { if (baskets[j].isFree) { minBasket = j; break; } } if (minBasket == -1) { // 没有空闲篮子,等待 ++currentTime; continue; } // 煮面 baskets[minBasket].isFree = false; baskets[minBasket].noodleType = order; baskets[minBasket].endTime = currentTime + noodleTimes[order]; orderTimes.push_back({ i + 1, baskets[minBasket].endTime }); currentTime = baskets[minBasket].endTime; }
// 输出送餐时间和篮子煮面数量 for (auto &time : orderTimes) { cout << time.first << ":" << time.second << " "; } cout << endl; for (int i = 0; i < M; ++i) { basketCounts[i + 1] = 0; } for (int i = 0; i < L; ++i) { int basketId = orders[i] - 1; basketCounts[basketId]++; } for (auto &count : basketCounts) { cout << count.second << " "; } cout << endl;
return 0;}


所有答案仅供参考,如有错误欢迎批评改正!

信奥导航
信息学奥赛自学规划、题目带刷答疑、测试一体化。
 最新文章