[CSP-J]2026年核桃-4 比赛总结

· · 个人记录

[CSP-J]2026年核桃-4 总结

比赛总分 400 pts,预估得分 160pts,实际得分 150pts

题号 题目 得分
T1 退格(backspace) 预估 100 pts,实得 100 pts
T2 竖式除法(division) 预估 60 pts,实际 35 pts
T3 断货邮票(stamps) 预估 0 pts,实得 15 pts
T4 归零(zero) 预估 0 pts,实得 0 pts

时间分配

T1很快就出来了,主要在T2上花了一些时间,T3、T4基本就骗了点分,没有什么空闲时间 \color{e8e8e8}{Mind+真好玩}

题目

T1

题目大意及思路

小H正从左到右输入字符串(长度为n,包含#),里面仅包含小写字母,但若出现#,则说明他按下了backspace,会删除输入的字符串内最后一个人字母,若该字符为小写字母,则直接插入字符串末尾,求最后的字符串

示例:

11
ab#c#de#fg

输入流程

a -> ab -> a -> ac -> a -> (空)-> d -> de -> d -> df -> dfg

思路

模拟即可

参考代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    freopen("backspace.in", "r", stdin);
    freopen("backspace.out", "w", stdout);
    int n;
    cin >> n;
    string s = "";
    for (int i = 1; i <= n; i++) {
        char c;
        cin >> c;
        if (islower(c)) s.push_back(c);
        else if (c == '#') {
            if (s.size() != 0) s.erase(s.size() - 1, 1);
        }
    }
    cout << s;
    return 0;
}

T2

题目大意及思路

给定被除数 a (小于等于 2000 位数),除数b(0 \le b \le9 ~~ b \le a),输出计算这个除法的竖式计算形式

例子:

input
1005
5
output
   201
5|1005
  10
  10
  --
    0
    0
    -
     5
     5
     -
     0

先用高精算出最终答案,然后通过类似高精的方式模拟即可

参考代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    freopen("division.in", "r", stdin);
    freopen("division.out", "w", stdout);
    string a;
    int b;
    cin >> a >> b;
    int x = 0;
    string res = "";
    for (int i = 0; i < a.size(); i++) {
        x = (x << 1) + (x << 3) + a[i] - '0';
        if (x >= b) {
            res.push_back(x / b + '0');
            x %= b;
        } else {
            res.push_back('0');
        }
    }
    while (res[0] == '0') res.erase(0, 1);
    int n = a.size(), m = res.size();
    for (int i = 1; i <= n - m + 2; i++) cout << ' ';
    cout << res << '\n' << b << '|' << a << '\n';
    int last = 0;
    for (int i = 0; i < n; i++) {
        int l = i;
        int now = (last << 1) + (last << 3) + a[i] - '0';
        if (i == 0) {
            while (now < b && i < n - 1) {
                i++;
                now = (now << 1) + (now << 3) + (a[i] - '0');
            }
        }
        if (last != 0) l--;
        int xx = (now / b) * b;
        string hh = to_string(xx);
        string h = to_string(now);
        for (int j = 1; j <= 2 + l; j++) cout << ' ';
        cout << now << '\n';
        for (int j = 1; j <= 2 + l + (h.size() - hh.size()); j++) cout << ' ';
        cout << xx << '\n';
        for (int j = 1; j <= 2 + l; j++) cout << ' ';
        for (int j = 1; j <= h.size(); j++) cout << '-';
        cout << '\n';
        last = now % b;
    }
    for (int i = 1; i <= n + 1; i++) cout << ' ';
    cout << x; 
    return 0;
}

T3

目前还没做出来

T4

题目大意及思路

给定一个长度为n的正整数序a_1,a_2,a_3 \dots a_n,你可以对这个序列进行 n - 1次操作,每次可以取出任意2个数字,再放入他们的和的绝对值(经过n - 1次操作后序列长度刚好变为1),最后需要只剩一个0,求操作的方案(输出每次进行操作的两个数,因为操作仅跟当前数字有关,跟位置无关在插入时无需考虑所在位置)

思路:

把序列放入优先队列(降序),每次将对头两个出队再插入它们的差即可(每次输出出对的两个数)

参考代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    freopen("zero.in", "r", stdin);
    freopen("zero.out", "w", stdout);
    priority_queue<int> q;
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        q.push(x);
    }
    while (q.size() >= 2) {
        int x = q.top();
        q.pop();
        int y = q.top();
        q.pop();
        cout << x << ' ' << y << '\n';
        q.push(x - y);
    }
    return 0;
}

考场代码

//T1 AC不展示

//T2 PAC(35pts)

#include <bits/stdc++.h>
using namespace std;
string a, b;
void solve1() {
    int n = a.size();
    int m = 1;
    cout << "  ";
    cout << a << '\n';
    cout << b << '|' << a << '\n';
    for (int i = 0; i < n; i++) {
        for (int j = 1; j <= 2 + i; j++) cout << ' ';
        cout << a[i] << '\n';
        for (int j = 1; j <= 2 + i; j++) cout << ' ';
        cout << a[i] << '\n';
        for (int j = 1; j <= 2 + i; j++) cout << ' ';
        cout << "-\n";
    }
    for (int i = 1; i < n + 1; i++) cout << ' ';
    cout << 0; 
}
void solve2() {
    int n = a.size();
    int m = b.size();
    long long A = stoll(a);
    long long y = stoll(b);
    long long xx = A / y;
    string rr = to_string(xx);
    for (int i = 1; i <= n - rr.size() + 2; i++) cout << ' ';
    cout << A / y << '\n';
    cout << b << '|' << a << '\n';
    int last = 0;
    for (int i = 0; i < n; i++) {
        int l = i;
        int x = (last << 1) + (last << 3) + a[i] - '0';
        if (x != 0) {
            while (x < y && i < n - 1) {
                i++;
                x = (x << 1) + (x << 3) + (a[i] - '0');
            }
        }
        if (last != 0) l--;
        for (int j = 1; j <= 2 + l; j++) cout << ' ';
        cout << x << '\n';
        for (int j = 1; j <= 2 + l; j++) cout << ' ';
        cout << (x / y) * y << '\n';
        for (int j = 1; j <= 2 + l; j++) cout << ' ';
        string h = to_string(x);
        for (int j = 1; j <= h.size(); j++) cout << '-';
        cout << '\n';
        last = x % y;
    }
    for (int i = 1; i <= n + 1; i++) cout << ' ';
    cout << 0; 
}
void solve3() {
    int n = a.size();
    int m = b.size();
    long long A = stoll(a);
    long long y = stoll(b);
    long long xx = A / y;
    string rr = to_string(xx);
    for (int i = 1; i <= n - rr.size() + 2; i++) cout << ' ';
    cout << A / y << '\n';
    cout << b << '|' << a << '\n';
    int last = 0;
    for (int i = 0; i < n; i++) {
        int l = i;
        int x = (last << 1) + (last << 3) + a[i] - '0';
        if (x != 0) {
            while (x < y && i < n - 1) {
                i++;
                x = (x << 1) + (x << 3) + (a[i] - '0');
            }
        }
        if (last != 0) l--;
        for (int j = 1; j <= 2 + l; j++) cout << ' ';
        cout << x << '\n';
        for (int j = 1; j <= 2 + l; j++) cout << ' ';
        cout << (x / y) * y << '\n';
        for (int j = 1; j <= 2 + l; j++) cout << ' ';
        string h = to_string(x);
        for (int j = 1; j <= h.size(); j++) cout << '-';
        cout << '\n';
        last = x % y;
    }
    for (int i = 1; i <= n + 1; i++) cout << ' ';
    cout << last;
}
int main() {
    freopen("division.in", "r", stdin);
    freopen("division.out", "w", stdout);
    cin >> a >> b;
    if (b == "1") {
        solve1();
        return 0;
    }
    if (a.size() <= 3) {
        solve2();
        return 0;
    }
    if (a.size() <= 18) {
        long long nn = stoll(a);
        long long bb = stoll(b);
        if (nn % bb == 0) solve2();
        else solve3();
    }
    return 0;
}

//T3骗分(输出大样例)不展示

//T4未写不展示

整体总结

T4有 20% 的情况完全不用循环就可以做,自己却没打,\color{ffffff}{实在不想打},T2写的骗部分分代码有些手滑写错了痛失15pts

改进:

\Huge{谢谢观看}