CSP认证题解第二题

tech2025-04-26  5

文章目录

202012-2 期末预测之最佳阈值202009-2 风险人群201909-2 小明种苹果(续)201903-2 二十四点201812-2 小明放学201809-2 买菜201803-2 碰撞的小球201703-2 学生排队201312-2 ISBN号码

202012-2 期末预测之最佳阈值

//70 #include <iostream> #include <stack> #define M 100000 using namespace std; typedef long long LL; int pre(int y, int the) { if(y < the) return 0; else return 1; } LL s1[M + 1]; int res[M + 1]; int main() { int m, sum = 0, i, j; //int y, result; stack<long long> s; cin >> m; for(i = 0; i < m; i++) { cin >> s1[i]; cin >> res[i]; s.push(s1[i]); } int max = 0, maxi = 0; for(j = 0; j < m; j++) { sum = 0; for(i = 0; i < m; i++) if(pre(s1[i], s.top()) == res[i]) sum++; if(sum > max) { max = sum; maxi = s.top(); } if(sum == max) { if(s.top() > maxi) maxi = s.top(); } s.pop(); } cout << maxi; return 0; }

202009-2 风险人群

0

#include <iostream> using namespace std; int main() { int n, k, t, xl, yl, xr, yr, x1, y1, i, j, max; int jsum = 0, dsum = 0; cin >> n >> k >> t >> xl >> yl >> xr >> yr; for (i = 0; i < n; i++) { max = 0; int cnt = 0, count = 0; for (j = 0; j < t; j++) { cin >> x1 >> y1; if (x1 >= xl && x1 <= xr && y1 >= yl && y1 <= yr) { count++; cnt++; } else { if (max < count) max = count; count = 0; } } if (max == 0) max = count; if (max > k) dsum++; if (cnt > 0) jsum++; } cout << jsum << endl << dsum << endl; return 0; }

100没啥好说的,经验就是分开做,别以为你能一次处理多个

#include<iostream> using namespace std; int arr[2001] = { 0 }; int main(void) { int n, k, t, xl, yd, xr, yu; scanf("%d%d%d%d%d%d%d", &n, &k, &t, &xl, &yd, &xr, &yu); int tempn = n, jingguo = 0, douliu = 0; while (tempn--) { for (int i = 1; i <= t; i++) scanf("%d%d", &arr[i], &arr[i + 1000]); int tempk = 0; for (int i = 1; i <= t; i++) if (arr[i] >= xl && arr[i] <= xr && arr[i + 1000] >= yd && arr[i + 1000] <= yu) { jingguo++; break; } for (int i = 1; i <= t; i++) { if (arr[i] >= xl && arr[i] <= xr && arr[i + 1000] >= yd && arr[i + 1000] <= yu) { tempk++; if (tempk == k) { douliu++; break; } } else tempk = 0; } } printf("%d\n%d\n", jingguo, douliu); return 0; }

201909-2 小明种苹果(续)

50分,空间占用382.8MB,太大了所以不给过???

#include <iostream> #define N 1000 using namespace std; int apple[N + 1][N + 1]; int flag[N + 1]; int main() { int n, t, d = 0, e = 0, sumt = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> t; for (int j = 0; j < t + 1; j++) if (j == 0) apple[i][j] = t; else cin >> apple[i][j]; } for (int i = 0; i < n; i++) { int sum = 0; for (int j = 0; j < apple[i][0] + 1; j++) { if (j == 0) continue; else if (j == 1) sum = apple[i][j]; else if (apple[i][j] <= 0) sum += apple[i][j]; else if (sum > apple[i][j]) { d++; sum = apple[i][j]; flag[i] = 1; } } sumt += sum; } if (d >= 3) for (int i = 0; i < n; i++) if (flag[(i - 1 + n) % n] && flag[i] && flag[(i + 1) % n]) e++; cout << sumt << " " << d << " " << e; return 0; }

100分,空间占用仅520.0kb

#include <iostream> #define N 1000 using namespace std; int flag[N + 1]; int main() { int n, t, a, d = 0, e = 0, sumt = 0; cin >> n; for (int i = 0; i < n; i++) { int sum = 0; cin >> t; cin >> sum; for (int j = 2; j <= t; j++) { cin >> a; if (a <= 0) sum += a; else if (a < sum) { flag[i] = 1; sum = a; } } sumt += sum; if (flag[i]) d++; } if (d >= 3) for (int i = 0; i < n; i++) if (flag[(i - 1 + n) % n] && flag[i] && flag[(i + 1) % n]) e++; cout << sumt << " " << d << " " << e; return 0; }

201903-2 二十四点

// no one can tell me why it is 0, so 我把它挂出来了TAT #include <stdio.h> #include <string.h> #include <math.h> int a[10]; char s[7]; int original(char s[]); bool issecond(char c); bool istwofour(char s[]); int original(char s[]) { int res = a[0]; for (int i = 1; i < 7; i += 2) { if (s[i] == '+') res += a[i + 1]; if (s[i] == '-') res -= a[i + 1]; if (s[i] == 'x') res *= a[i + 1]; if (s[i] == '/') res += floor(res / a[i + 1]); } return res; } bool issecond(char c) { if (c == 'x' || c == '/') return true; else return false; } bool istwofour(char s[]) { int res = 0; for (int i = 0; i < 7; i += 2) a[i] = s[i] - '0'; if (issecond(s[1]) && issecond(s[3]) && issecond(s[5])){ if (s[1] == 'x') res = a[0] * a[2]; else res = floor(a[0] / a[2]); for (int i = 3; i < 7; i += 2) if (s[i] == 'x') res = res * a[i + 1]; else res = floor(res / a[i + 1]); } else if (!(issecond(s[1])) && !(issecond(s[3])) && !(issecond(s[5]))){ res += a[0]; for (int i = 1; i < 7; i += 2) if (s[i] == '+') res += a[i + 1]; else res -= a[i + 1]; } else { int m = 0, n = 0; for (int i = 3; i < 7; i += 2) if (issecond(s[i])) { m = i; if (i < 4) { i += 2; if (issecond(s[i])) n = i; } } if (m == 0) res = original(s); else if (n == 0) { if (issecond(s[1])) { int tmp; res = a[0]; if (s[1] == '/') res = floor(res / a[2]); else res *= a[2]; if (s[5] == '/') tmp = floor(a[4] / a[6]); else tmp = a[4] * a[6]; if (s[3] == '+') res += tmp; else res -= tmp; } else { if (s[5] == '/') res += floor(a[4] / a[6]); else res += a[4] * a[6]; if (s[3] == '+') res += a[2]; else res += a[2] - res; if (s[1] == '+') res += a[0]; else res += a[0] - res; } } else { if (s[3] == '/') res += floor(a[2] / a[4]); else res += a[2] * a[4]; if (s[5] == '/') res = floor(res / a[6]); else res = res * a[6]; if (s[1] == '+') res += a[0]; else res += a[0] - res; } } return res == 24; } int main() { int n; scanf("%d", &n); while (n--) { scanf("%s", s); if (istwofour(s)) printf("Yes\n"); else printf("No\n"); } return 0; }

201812-2 小明放学

第一次写只有30分,就不贴了,第二次写60分呵呵第二题真难,第三次80了,但是我不知道哪里还有瑕疵。

#include <iostream> using namespace std; int main() { int r, y, g, n, k, t, sum1; long long sum = 0, judge; // 第一次没看清题目要求,用了4字节的整型导致60分 cin >> r >> y >> g >> n; sum1 = r + g + y; while (n--) { cin >> k >> t; judge = (sum - t) % sum1; switch (k) { case 1: if (judge >= g + y) sum = sum + sum1 - judge; else if (judge >= g) sum = sum + g + y - judge + r; break; case 2: if (judge <= r) sum = sum + r - judge; else if (judge >= r + g) sum = sum + sum1 - judge + r; break; case 3: if (judge <= y + r) sum = sum + r + y - judge; break; default: sum += t; break; } } cout << sum << endl; return 0; }

201809-2 买菜

// 60points #include <iostream> #define N 1000000 using namespace std; struct stucktime { int le, ri; }; stucktime xh[N + 1], xw[N + 1]; int main() { int n, i, j, sum = 0; cin >> n; for (i = 0; i < n; i++) cin >> xh[i].le >> xh[i].ri; for (i = 0; i < n; i++) cin >> xw[i].le >> xw[i].ri; for (i = 0; i < n; i++) for (j = 0; j < n; j++) if (xh[i].le < xw[j].le) { if (xh[i].ri > xw[j].le && xh[i].ri < xw[j].ri) sum += xh[i].ri - xw[j].le; else if (xh[i].ri > xw[j].ri) sum += xw[j].ri - xw[j].le; } else if (xh[i].le >= xw[j].le && xh[i].le <xw[j].ri) { if (xh[i].ri <= xw[j].ri) sum += xh[i].ri - xh[i].le; else sum += xw[j].ri - xh[i].le; } cout << sum; return 0; } #include <iostream> #define N 1000000 using namespace std; // 声明结构体,包含两个人装车时间的左和右 struct stucktime { int le, ri; }; stucktime xh[N + 1], xw[N + 1]; int main() { int n, i, j, sum = 0, max, min; cin >> n; for (i = 0; i < n; i++) cin >> xh[i].le >> xh[i].ri; for (i = 0; i < n; i++) cin >> xw[i].le >> xw[i].ri; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { // 找到两个人的空闲时间 if (xh[i].le > xw[j].le) max = xh[i].le; else max = xw[j].le; if (xh[i].ri < xw[j].ri) min = xh[i].ri; else min = xw[j].ri; // 如果max比min大那么两个人没有时间交流 if (max < min) sum += min - max; } cout << sum; return 0; }

201803-2 碰撞的小球

#include <iostream> #include <cmath> #define N 100 using namespace std; int loc[N + 1]; // 每个小球的位置 int main() { int i, j, n, l, t, m; cin >> n >> l >> t; for (i = 0; i < n; i++) cin >> loc[i]; for (i = 0; i < t; i++) for (j = 0; j < n; j++) { // 如果碰到了墙将返回 if (abs(loc[j]) == l) loc[j] = -loc[j]; // 两个球刚好碰撞则都向相反的方向走 else for(m = j+1; m < n; m++) if (abs(loc[j]) == abs(loc[m])) { loc[j] = -loc[j]; loc[m] = -loc[m]; } loc[j]++; } // 报出每个球的最终位置 for (i = 0; i < n; i++) { loc[i] = abs(loc[i]); if (i != 0) cout << " "; cout << loc[i]; } return 0; }

201703-2 学生排队

#include <iostream> #define N 2000 using namespace std; int stu[N + 1]; void MakeTable(int); void swap(int*, int*); void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } void MakeTable(int n) { for (int i = 1; i <= n; i++) stu[i] = i; } int main() { int n, m, s1, s2, a; cin >> n >> m; MakeTable(n); while (m--) { cin >> s1 >> s2; for (int i = 1; i <= n; i++) if (stu[i] == s1) { a = i; break; } while (s2 != 0) { if (s2 > 0) { swap(&stu[a], &stu[a + 1]); s2--, a++; } else { swap(&stu[a], &stu[a - 1]); s2++, a--; } } } for (int i = 1; i <= n; i++) { if (i != 1) cout << " "; cout << stu[i]; } return 0; }

201312-2 ISBN号码

// 40points, i don't know what do i write \=_=/ #include <iostream> using namespace std; int main() { int a, b, c, d, sum = 0, t, cos_d, cos_c, cos_b; scanf("%d-%d-%d-%d", &a, &b, &c, &d); sum += a; cos_b = b; cos_c = c; for (int i = 4; i > 1; i--) { t = b % 10; sum += t * i; b /= 10; } for (int i = 9; i > 4; i--) { t = c % 10; sum += t * i; c /= 10; } cos_d = sum % 11; if (cos_d == d) cout << "Right!"; else printf("%d-%d-%d-%d", a, cos_b, cos_c, cos_d); return 0; } // 50points i look tijie then do,but it didn't give me nice grade. #include <iostream> #include <string> using namespace std; int main() { int sum = 0, i = 0, judge; string s; getline(cin, s); for (int j = 0; j < 12; j++) { if (s[j] == '-') continue; i++; sum += (s[j] - '0') * i; } judge = sum % 11; // 这里如果judge = 10,需要用X来代替,没有考虑到这一点导致错误 if (judge == s[12] - '0') cout << "Right"; else { s[12] = judge + '0'; for (string::iterator it = s.begin(); it != s.end(); it++) cout << *it; } return 0; } #include <iostream> #include <string> using namespace std; int main() { int sum = 0, i = 0; string s, mod; mod = "0123456789X"; getline(cin, s); for (int j = 0; j < 12; j++) { if (s[j] == '-') continue; i++; sum += (s[j] - '0') * i; } if (s[12] == mod[sum % 11]) cout << "Right"; else { s[12] = mod[sum % 11]; for (string::iterator it = s.begin(); it != s.end(); it++) cout << *it; } return 0; }
最新回复(0)