2021 RoboCom 世界机器人开发者大赛-高职组(初赛)解题报告 | 珂学家
前言
题解
2021 RoboCom 世界机器人开发者大赛-高职组(初赛。
整体比较基础,但是7-5 增一数这题数据有点小坑。
7-1 机器人打招呼
分值: 5分
hello world系列
#include <bits/stdc++.h>using namespace std;int main() {cout << "ni ye lai can jia RoboCom a?\n";return 0;
}
7-2 人脸识别
分值: 10分
模拟即可
#include <bits/stdc++.h>using namespace std;int main() {int arr[3], brr[3];for (int i = 0; i < 3; i++) cin >> arr[i];int T; cin >> T;for (int i = 0; i < 3; i++) cin >> brr[i];cout << "Diff = ";int diff = 0;for (int i = 0; i < 3; i++) {if (i > 0) cout << ", ";cout << arr[i] - brr[i];diff += abs(arr[i] - brr[i]);}cout << "\n" << (diff <= T ? "Yes" : "No") << "\n";return 0;
}
7-3 月份输出
分值: 10分
打表的技巧
#include <bits/stdc++.h>using namespace std;const char *str[] = {"January","February","March","April","May","June","July","August","September","October","November","December"
};int main() {int v;while (cin >> v && v >= 1 && v <= 12) {cout << str[v - 1] << "\n";}cout << "?\n";return 0;
}
7-4 字母串
分值: 15分
模拟,字符串的题,喜欢考察转换(大小写字母,前后转)
#include <bits/stdc++.h>using namespace std;bool check(string &s) {int n = s.length();for (int i = 0;i < n - 1; i++) {char ch = s[i], nch = s[i + 1];if (ch >= 'a' && ch <= 'z') {if (nch != (ch - 'a' + 'A') && ch != nch + 1) {return false;}} else {if (nch != (ch - 'A' + 'a') && ch != nch - 1) {return false;}}}return true;
}int main() {int t;cin >> t;while (t-- > 0) {string s;cin >> s;cout << (check(s) ? "Y" : "N") << "\n";}return 0;
}
7-5 增一数
分值: 15分
难得的数论题
如何求解某个数是否为平方数
- sqrt求解,可以用库函数sqrt,也可以二分逼近,然后在平方check
- 因子拆解,时间复杂度为 O ( n ) O(\sqrt n) O(n)
需要注意的是:
这题测试数据,存在多余的前置0,需要去掉,不然会少一分。
#include <bits/stdc++.h>using namespace std;// 因子拆解
bool check(int64_t v) {for (int i = 2; i <= v / i; i++) {if (v % i == 0) {int cnt = 0;while (v % i == 0) {v /= i;cnt++;}if (cnt % 2 == 1) return false;}}if (v > 1) return false;return true;
}int main() {int t;cin >> t;while(t-- > 0) {string v; cin >> v;// 存在特殊的前置0,需要过滤掉while (v.length() > 0 && v[0] == '0') {v = v.substr(1);}int n = v.length();if (n % 2==1) {cout << 0 << "\n";continue;}int a1 = stoi(v.substr(0, n / 2));int a2 = stoi(v.substr(n/2));if (a1 + 1 == a2) {int u = stoi(v);// 库函数 平方根int r = (int)sqrt(u);// if (check(u)) {if (r * r == u) {cout << 2 << "\n";} else {cout << 1 << "\n";}} else {cout << 0 << "\n";}}return 0;
}
7-6 答题卡
分值: 20分
模拟+解析表达式
需要注意坐标:
左下是 ( 1 , 1 ) , 右上是 ( n , n ) 左下是(1,1), 右上是(n, n) 左下是(1,1),右上是(n,n)
#include <bits/stdc++.h>using namespace std;int main() {int n, m;cin >> n >> m;vector<string> g(n);string s;for (int i = 0; i < n; i++) {s += '.';}for (int i = 0; i < n; i++) g[i] = s;for (int i = 0; i < m; i++) {string s; cin >> s;if (s.find(';') != string::npos) {int p = s.find(';');int x = stoi(s.substr(0, p));int y = stoi(s.substr(p + 1));g[n - y][x - 1] = '#';} else if (s.find("/") != string::npos) {int p = s.find("/");int x = stoi(s.substr(0, p));int y = stoi(s.substr(p + 1));g[n - y][x - 1] = '#';} else {while (s.length() < 4) {s = "0" + s;}int x = stoi(s.substr(0, 2));int y = stoi(s.substr(2));g[n - y][x - 1] = '#';}}for (int i = 0; i < n; i++) cout << g[i] << "\n";return 0;
}
7-7 救救倒霉鬼
分值: 25分
这题反而简单,集合的差集
#include <bits/stdc++.h>using namespace std;int main() {int n;set<string> st;cin >> n;for (int i = 0; i < n;i++) {string s; cin >> s;st.insert(s);}int m; cin >> m;for (int i = 0; i < m; i++) {string s; cin >> s;st.erase(s);}auto iter = st.rbegin();while (iter != st.rend()) {cout << *iter << "\n";iter++;}return 0;
}
set的逆序遍历,可以借助rbegin和rend对应的逆序迭代器来实现。