题目见这
试题C 数列求值
#include <iostream>
using namespace std;
char str[27] = {'0', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q','R', 'S', 'T',
'U', 'V','W', 'X', 'Y', 'Z'};
int main() {
int num = 2019;
string ans = "";
while (num) {
ans += str[num % 26];
num /= 26;
}
for (int i = ans.size() - 1; i >= 0; i--) {
printf("%c", ans[i]);
}
return 0;
}
试题D 数的分解
#include <cstdio>
using namespace std;
typedef long long LL;
bool st[2019];
int main() {
int res = 0;
for (int i = 2; i <= 2019; i++) {
int t = i;
while (t) {
int k = t % 10;
if (k == 2 || k == 4) {
st[i] = true;
break;
}
t /= 10;
}
}
for (int i = 1; i < 2019; i++)
for (int j = i + 1; j < 2019; j++) {
int k = 2019 - i - j;
if (k == 0 || k <= i || k <= j) continue;
if (st[i] || st[j] || st[k]) continue;
res++;
}
printf("%d\n", res);
return 0;
}
试题E 迷宫
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 50;
char g[N][N];
bool st[N][N];
int n, m;
struct Node {
int x;
int y;
string path;
};
int dx[4] = {1, 0, 0, -1}, dy[4] = {0, -1, 1, 0};
char dir[4] = {'D', 'L', 'R', 'U'};
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%s", g[i] + 1);
queue<Node> q;
q.push({1, 1, ""});
st[1][1] = true;
while (!q.empty()) {
Node t = q.front();
q.pop();
if (t.x == n && t.y == m) {
cout << t.path;
break;
}
for (int i = 0; i < 4; i++) {
int a = t.x + dx[i], b = t.y + dy[i];
if (a <= 0 || b <= 0 || a > n || b > m) continue;
if (st[a][b]) continue;
if (g[a][b] == '1') continue;
q.push({a, b, t.path + dir[i]});
st[a][b] = true;
}
}
return 0;
}
试题F 特别数的和
#include <cstdio>
using namespace std;
int n;
int main() {
scanf("%d", &n);
int res = 0;
for (int i = 1; i <= n; i++) {
int t = i;
while (t) {
int k = t % 10;
if (k == 2 || k == 0 || k == 1 || k == 9) {
res += i;
break;
}
t /= 10;
}
}
printf("%d\n", res);
return 0;
}
试题G 完全二叉树的权值
#include <iostream>
using namespace std;
const int N = 100010;
int a[N];
int n;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
int len = 1;
int l = 1, r = 1;
int max_line = 0;
int line = 1;
int res = 0;
while (true) {
int sum = 0;
int i = l;
for (; i <= min(r, n); i++) sum += a[i];
if (res < sum) {
res = max(res, sum);
max_line = line;
}
if (i > n) break;
len *= 2;
line ++;
l = r + 1;
r = r + len;
}
printf("%d\n", max_line);
return 0;
}
试题H 等差数列
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
int a[N];
int n;
int gcd(int a, int b) {
return b ? gcd(b, a % b): a;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
sort(a, a + n);
int min_gcd = 0;
for (int i = 1; i < n; i++) {
min_gcd = gcd(min_gcd, a[i] - a[0]);
if (!min_gcd) {
printf("%d", n);
return 0;
}
}
printf("%d\n", (a[n - 1] - a[0]) / min_gcd + 1);
return 0;
}
试题I 后缀表达式
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 100010;
int n, m;
int a[N + N];
int main() {
scanf("%d%d", &n, &m);
int num = n + m + 1;
for (int i = 0; i < num; i++) scanf("%d", &a[i]);
LL res = 0;
if (!m) {
for (int i = 0; i < num; i++)
res += a[i];
} else {
sort(a, a + num);
res = res + a[num - 1] - a[0];
for (int i = 1; i < num - 1; i++)
res += abs(a[i]);
}
printf("%lld\n", res);
return 0;
}
试题J 灵能传输
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 300010;
int n;
LL a[N], s[N];
bool st[N];
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
s[i] = s[i - 1] + a[i];
}
LL s0 = s[0], sn = s[n];
if (s0 > sn) swap(s0, sn);
sort(s, s + n + 1);
for (int i = 0; i <= n; i++)
if (s[i] == s0) {
s0 = i;
break;
}
for (int i = n; i >= 0; i--)
if (s[i] == sn) {
sn = i;
break;
}
memset(st, 0, sizeof st);
int l = 0, r = n;
for (int i = s0; i >= 0; i -= 2) {
a[l++] = s[i];
st[i] = true;
}
for (int i = sn; i <= n; i += 2) {
a[r--] = s[i];
st[i] = true;
}
for (int i = 0; i <= n; i++)
if (!st[i])
a[l++] = s[i];
LL res = 0;
for (int i = 1; i <= n; i++) res = max(res, abs(a[i] - a[i - 1]));
printf("%lld\n", res);
}
return 0;
}