The eighth day of intensive training at Nanjing Foreign Language School.
第一题,模拟
#include <bits/stdc++.h>
using namespace std;
int a[17], ans = 4, cnt, tot;
int main() {
for (int i = 1; i <= 16; i++) {
cin >> a[i];
}
for (int i = 1; i <= 4; i++) {
cnt = 0;
tot = 0;
for (int j = 1; j <= 16; j++) {
if (a[j] > cnt) {
cnt = a[j];
tot = j;
}
}
a[tot] = -1;
}
for (int i = 1; i <= 16; i++) {
if (abs(cnt - a[i]) < 300) {
ans++;
}
}
cout << ans;
return 0;
}
第二题,数学题
#include <bits/stdc++.h>
using namespace std;
int a[17], ans = 4, cnt, tot;
int main() {
for (int i = 1; i <= 16; i++) {
cin >> a[i];
}
for (int i = 1; i <= 4; i++) {
cnt = 0;
tot = 0;
for (int j = 1; j <= 16; j++) {
if (a[j] > cnt) {
cnt = a[j];
tot = j;
}
}
a[tot] = -1;
}
for (int i = 1; i <= 16; i++) {
if (abs(cnt - a[i]) < 300) {
ans++;
}
}
cout << ans;
return 0;
}
第三题,循环枚举寻找最大公因数
#include <bits/stdc++.h>
using namespace std;
long long n, x, p[2020020], c[2020020], tot = 2147483647;
bool b = false;
int main() {
cin >> n >> x;
for (int i = 1; i <= n; i++) {
cin >> p[i];
c[i] = abs(x - p[i]);
tot = min(c[i], tot);
}
while (tot > 1) {
b = true;
for (int i = 1; i <= n; i++) {
if (c[i] % tot != 0) {
b = false;
break;
}
}
if (b == true) {
break;
} else {
tot--;
}
}
cout << tot;
return 0;
}
第四题,递归合并
#include <bits/stdc++.h>
using namespace std;
int n, A, B, C, l[999], ans = 0x3f3f3f3f;
void dfs(int q, int x, int y, int z, int p) {
if (x && y && z) {
ans = min(ans, abs(x - A) + abs(y - B) + abs(z - C) + p - 30);
}
if (q > n) {
return;
}
dfs(q + 1, x + l[q], y, z, p + 10);
dfs(q + 1, x, y + l[q], z, p + 10);
dfs(q + 1, x, y, z + l[q], p + 10);
dfs(q + 1, x, y, z, p);
}
int main() {
cin >> n >> A >> B >> C;
for (int i = 1; i <= n; i++) {
cin >> l[i];
}
dfs(0, 0, 0, 0, 0);
cout << ans;
return 0;
}