携程笔试最后一题
前三道都ac了,最后一道卡在20,求大佬看看为啥
最后一题
#include <bits/stdc++.h> using namespace std; unsigned long long mod = 1e9+7; int main() { int n; cin >> n; unsigned long long pow = 20; if (n == 2) { cout << pow; return 0; } unsigned long long side = (4 * n + 1) % mod; side = (side * (2 * n - 4)) % mod; side = (side * 3) % mod; pow += side; unsigned long long mid = ((n - 2) * (n - 2)) % mod; unsigned long long temp = (n * n + 4 * n - 3) % mod; mid = (mid * temp) % mod; mid = (mid * 2) % mod; pow += mid; pow = pow % mod; cout << pow; return 0; }
第一题
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; float s = 0.0; if (k % 2 == 0) { float parts = k / 2 + 1; s = n / parts * n / parts; } else { float partsw = (k - 1) / 2 + 1; float partsl = (k + 1) / 2 + 1; s = n * n / (partsw * partsl); } printf("%0.2f", s); return 0; }第二题,一堆if else
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { vector<int> mata(4), matb(4); for (int j = 0; j < 4; j++) cin >> mata[j]; for (int j = 0; j < 4; j++) cin >> matb[j]; int check = 0; for (int j = 0; j < 4; j++) { if ((mata[j] != 0 && mata[j] != 1) || (matb[j] != 0 && matb[j] != 1)) { check = 1; break; } } if (check) { cout << "-1" << endl; continue; } int na1 = 0, nb1 = 0; for (int j = 0; j < 4; j++) { if (mata[j] == 1) na1++; if (matb[j] == 1) nb1++; } if (na1 != nb1) { cout << "-1" << endl; continue; } if (na1 == 0 || na1 == 4) { cout << "0" << endl; continue; } vector<int> matemp(4); int sum = 0; for (int j = 0; j < 4; j++) { matemp[j] = abs(mata[j] - matb[j]); sum += matemp[j]; } if (sum == 0) { cout << "0" << endl; } else if ((matemp[0] == 1 && matemp[3] == 1) || (matemp[1] == 1 && matemp[2] == 1)) { cout << "2" << endl; continue; } else { cout << "1" << endl; continue; } } return 0; }第三题
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int n; cin >> n; vector<int> a(n); for (int j = 0; j < n; j++) cin >> a[j]; string s; cin >> s; int maxr = 0, maxb = 0, minr = 1e9 + 1, minb = 1e9 + 1; for (int j = 0; j < n; j++) { if (s[j] == 'R') { maxr = max(a[j], maxr); minr = min(a[j], minr); } else { maxb = max(a[j], maxb); minb = min(a[j], minb); } } if (maxr == 0) { cout << maxb - minb << endl; continue;; } if (maxb == 0) { cout << maxr - minr << endl; continue; } if (maxr >= maxb && minr >= minb) { cout << maxr - minb << endl; continue; } int difr = maxr - minr; int difb = maxb - minb; cout << max(difr, difb) << endl; } return 0; }