比赛链接:https://pintia.cn/market/item/1103202531901689856
7-1 大美数
题解
模拟即可。
Tips
这题通过率只有 $0.09$,可能是大部分人搞错了整除的关系,$a$ 整除 $b$ 表示为 $b\ \%\ a = 0$ 。
代码
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n; cin >> n;
vector<int> d;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
d.push_back(i);
if (n / i != i) d.push_back(n / i);
}
}
for (int i = 0; i < d.size(); i++) {
for (int j = i + 1; j < d.size(); j++) {
for (int k = j + 1; k < d.size(); k++) {
for (int l = k + 1; l < d.size(); l++) {
if ((d[i] + d[j] + d[k] + d[l]) % n == 0) {
cout << "Yes" << "\n";
return;
}
}
}
}
}
cout << "No" << "\n";
}
int main() {
int t; cin >> t;
while (t--) solve();
}
7-2 矩阵行平移
题解
模拟即可。
Tips
奇数行是依次向右平移 $1,2,\dots,k$ 个单位,不是 $1,k,1,k,\dots$ 。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, x; cin >> n >> k >> x;
int col[n] = {};
for (int i = 1, mov = 1; i <= n; i++) {
int a[n] = {};
for (int j = 0; j < n; j++)
cin >> a[j];
if (i & 1) {
rotate(a, a + n - mov, a + n);
fill(a, a + mov, x);
if (++mov == k + 1) mov = 1;
}
for (int j = 0; j < n; j++)
col[j] += a[j];
}
for (int i = 0; i < n; i++)
cout << col[i] << " \n"[i == n - 1];
}
7-3 岩洞施工
题解
枚举通道的高度和起点,需要削掉的高度即最高的底部高度和最低的顶部高度与通道的高度差之和。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
int n; cin >> n;
int h[n] = {};
for (int i = 0; i < n; i++)
cin >> h[i];
int l[n] = {};
for (int i = 0; i < n; i++)
cin >> l[i];
int ans = -1, del = INT_MAX;
for (int dia = 1; dia <= 1000; dia++) {
for (int st = 0; st <= 1000; st++) {
int mx = *max_element(l, l + n);
int mi = *min_element(h, h + n);
if (mx > st or mi < st + dia)
del = min(del, max(0, mx - st) + max(0, st + dia - mi));
else
ans = max(ans, dia);
}
}
cout << (ans != -1 ? "Yes " : "No ")
<< (ans != -1 ? ans : del) << "\n";
}
7-4 性感素数
题解
模拟即可。
代码
#include <bits/stdc++.h>
using namespace std;
bool isprime(int n) {
if (n < 2) return false;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
bool ok(int n) {
return isprime(n) and (isprime(n - 6) or isprime(n + 6));
}
int main() {
int n; cin >> n;
if (ok(n)) {
cout << "Yes" << "\n"
<< (isprime(n - 6) ? n - 6 : n + 6);
} else {
cout << "No" << "\n";
for (int i = n + 1; true; i++) {
if (ok(i)) {
cout << i << "\n";
return 0;
}
}
}
}
7-5 校庆
题解
将每个身份证号的出生日期排序并与原身份证号相关联即可。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
int n; cin >> n;
map<string, bool> mp;
for (int i = 0; i < n; i++) {
string s; cin >> s;
mp[s] = true;
}
int ans = 0;
set<string> st1, st2;
map<string, string> mp1, mp2;
int m; cin >> m;
for (int i = 0 ; i < m; i++) {
string s; cin >> s;
string t = s.substr(6, 8);
if (mp[s]) {
++ans;
st1.insert(t);
mp1[t] = s;
} else {
st2.insert(t);
mp2[t] = s;
}
}
cout << ans << "\n"
<< (ans > 0 ? mp1[*st1.begin()] : mp2[*st2.begin()]) << "\n";
}
来源:oschina
链接:https://my.oschina.net/u/4306931/blog/4417235