VMWare 9.21笔试
第一题 AC
#include <bits/stdc++.h> using namespace std; int main() { int m; double n, r; string s1, s2; cin >> s1 >> m >> s2; n = stod(s1); //printf("%f\n", n); //cout << n << endl; r = stod(s2); int x, y; for (int i = 1; i <= m; ++i) { double cur = i * 1.0 * r; int times = cur / (4.0 * n); //cout << times << endl; cur = cur - times * 4.0 * n; //cout << cur << endl; if (cur >= 0 && cur <= n * 1.0) { //cout << 1 << endl; printf("%.2f %.2f\n", cur, 0.0); } else if (cur > n * 1.0 && cur <= n * 2.0) { //cout << 2 << endl; printf("%.2f %.2f\n", n * 1.0, cur - n); } else if (cur > n * 2.0 && cur <= n * 3.0) { //cout << 3 << endl; printf("%.2f %.2f\n", 3.0 * n - cur, n * 1.0); } else { //cout << 4 << endl; printf("%.2f %.2f\n", 0.0, 4.0 * n - cur); } } }
第二题 AC
#include <bits/stdc++.h> using namespace std; int main() { int T, n; cin >> T; while (T--) { cin >> n; vector<int> pre(n); vector<int> next(n); for (int i = 0; i < n; ++i) cin >> pre[i]; for (int i = 0; i < n; ++i) cin >> next[i]; vector<int> simp; simp.push_back(next[0]); for (int i = 1; i < n; ++i) { if (next[i] == next[i - 1]) continue; simp.push_back(next[i]); } int i = 0; int j = 0; while (i < simp.size() && j < n) { if (simp[i] == pre[j]) { ++i; } ++j; } if (i == simp.size()) cout << "YES" << endl; else cout << "NO" << endl; } }
第三题 暴力 n的三次方,45%
后来想起来可以直接统计i和j的度,如果有i到j的边,就减去1,用了unordered_set存边(pair),编译失败。。。
查了一下,原来unordered_set不能直接存pair https://blog.csdn.net/zhaohaibo_/article/details/90340120 改成用二维vector,贴个代码
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> PAIR; int main() { int n, k; cin >> n >> k; vector<int> first(n); vector<int> second(n); vector<vector<char>> edge(n + 1, vector<char>(n + 1, 0)); vector<int> times(n + 1, 0); for (int i = 0; i < n; ++i) { cin >> first[i] >> second[i]; ++times[first[i]]; ++times[second[i]]; edge[first[i]][second[i]] = 1; edge[second[i]][first[i]] = 1; } int res = 0; for (int i = 1; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { int count = times[i] + times[j]; if (edge[i][j] == 1) --count; if (count >= k) ++res; } } cout << res << endl; }