前言
题解
2021 RoboCom 世界机器人开发者大赛-高职组(复赛)解题报告。
模拟题为主,包含进制转换等等。
最后一题,是对向量/自定义类型,重定义小于操作符。
7-1 人工智能打招呼
分值: 15分
考察点: 分支判定,判重技巧
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
set<string> hp;
for (int i = 0; i < n; i++) {
string id; cin >> id;
if (hp.find(id) == hp.end()) {
cout << "Hello " << id << ", how are you?\n";
hp.insert(id);
} else {
cout << "Hi " << id<< "! Glad to see you again!\n";
}
}
return 0;
}
7-2 人工智能数字翻译
分值: 20分
考察:进制转换,10和27进制互转
#include <bits/stdc++.h>
using namespace std;
int main() {
string a;
int d;
cin >> a >> d;
if (d == 10) {
string res;
int s = stoi(a);
while (s > 0) {
int r = s % 27;
if (r >= 0 && r < 10) {
res += (char)(r + '0');
} else {
res += (char)(r - 10 + 'A');
}
s /= 27;
}
if (res.empty()) res = "0";
reverse(res.begin(), res.end());
// 去掉前置多余的0
if (res[0] == '0' && res.size() > 0) {
int pos = res.size() - 1;
for (int i = 0; i < res.size(); i++) {
if (res[i] != '0') {
pos = i;
break;
}
}
res = res.substr(pos);
}
cout << res << "\n";
} else {
int64_t res = 0;
string s = a;
for (char c: s) {
if (c >= '0' && c <= '9') {
res = res * 27 + (c - '0');
} else {
res = res * 27 + (c - 'A' + 10);
}
}
cout << res << "\n";
}
return 0;
}
7-3 机器人拼图
分值: 20分
模拟
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> mat(n, vector<int>(m, 0));
for (int i = 0; i < n * m; i++) {
string op; cin >> op;
int x = 0, y = 0;
for (char c: op) {
if (c == '1') {
y = min(y + 1, m - 1);
} else if (c == '3') {
y = max(y - 1, 0);
} else if (c == '2') {
x = min(x + 1, n - 1);
} else if (c == '4') {
x = max(x - 1, 0);
} else {
if (mat[x][y] == 0) {
mat[x][y] = i + 1;
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << mat[i][j] << " \n"[j == m - 1];
}
}
return 0;
}
7-4 PAT基础级排名
分值: 20分
思路: 排序 + 分组循环
因为存在同分的情况,所以分组循环,是种优雅的写法
#include <bits/stdc++.h>
using namespace std;
struct Stu {
string name;
int score;
};
int main() {
int n, L;
cin >>n >> L;
vector<Stu> arr;
for (int i = 0; i < n; i++) {
Stu stu;
cin >> stu.name >> stu.score;
arr.push_back(stu);
}
sort(arr.begin(), arr.end(), [](auto &a, auto &b) {
if (a.score != b.score) return a.score > b.score;
return a.name < b.name;
});
int levels[6] = {0, 30, 50, 60, 80, 100};
auto resolve = [levels](int a) {
for (int i = 5; i >= 0; i--) {
if (a > levels[i]) return i + 1;
}
return 0;
};
int x = L + 1;
int m = n;
int acc = 0;
int rank = 0;
for (int i = 0;i < n; i++) {
Stu &stu = arr[i];
int level = resolve(stu.score);
if (x > level) {
m = m - acc;
acc = 0;
x = level;
rank = 1;
}
acc++;
if (acc > 1 && arr[i].score == arr[i - 1].score) {
} else {
rank = acc;
}
if (x != 0) {
cout << stu.name << " " << x << " "
<< stu.score << "/" << levels[x] << " "
<< rank << "/" << m << "\n";
} else {
cout << stu.name << "\n";
}
}
return 0;
}
7-5 人工智能刑警
分值: 25分
思路: 自定义类型重定义小于操作符
因为这边采用map(底层是树),所以需要对自定义类型重定义小于操作符。
#include <bits/stdc++.h>
using namespace std;
struct T {
vector<int> feature;
bool operator<(const T&lhs) const {
int n = feature.size();
for (int i = 0; i < n; i++) {
int v1 = feature[i], v2 = lhs.feature[i];
if (v1 != v2) return v1 < v2;
}
return 0;
}
};
int main() {
map<T, string> hp;
int n, f;
cin >> n >> f;
for (int i = 0; i < n; i++) {
T t;
for (int j = 0; j < f; j++) {
int v; cin >> v;
t.feature.push_back(v);
}
string name; cin >> name;
hp[t] = name;
}
while (true) {
bool ok = true;
T t2;
for (int i = 0; i < f; i++) {
int v;
if (cin >> v && (v == 0 && i == 0)) {
ok = false;
break;
}
t2.feature.push_back(v);
}
if (!ok) break;
if (hp.find(t2) != hp.end()) {
cout << hp[t2] << "\n";
} else {
cout << "Pass" << "\n";
}
}
return 0;
}