字符测试

· · 个人记录

1.P1553 数字反转(升级版)

简要思路分析

先判断输入字符串属于哪种类型(整数、小数、分数、百分数),然后分情况处理:

反转使用 reverse(),删除前导/末尾零用循环判断。

完整代码

#include <bits/stdc++.h>
using namespace std;

int main() {
    string s;
    cin >> s;

    // 小数
    if (s.find('.') != string::npos) {
        int pos = s.find('.');
        string A = s.substr(0, pos);
        string B = s.substr(pos + 1);

        reverse(A.begin(), A.end());
        while (A.size() > 1 && A[0] == '0') A.erase(A.begin());

        reverse(B.begin(), B.end());
        while (B.size() > 1 && B.back() == '0') B.pop_back();

        cout << A << "." << B;
        return 0;
    }

    // 分数
    if (s.find('/') != string::npos) {
        int pos = s.find('/');
        string fenzi = s.substr(0, pos);
        string fenmu = s.substr(pos + 1);

        reverse(fenzi.begin(), fenzi.end());
        while (fenzi.size() > 1 && fenzi[0] == '0') fenzi.erase(fenzi.begin());

        reverse(fenmu.begin(), fenmu.end());
        while (fenmu.size() > 1 && fenmu[0] == '0') fenmu.erase(fenmu.begin());

        cout << fenzi << "/" << fenmu;
        return 0;
    }

    // 百分数
    if (s.back() == '%') {
        string A = s.substr(0, s.size() - 1);
        reverse(A.begin(), A.end());
        while (A.size() > 1 && A[0] == '0') A.erase(A.begin());
        cout << A << "%";
        return 0;
    }

    // 整数
    reverse(s.begin(), s.end());
    while (s.size() > 1 && s[0] == '0') s.erase(s.begin());
    cout << s;

    return 0;
}

2.P1603斯诺登的密码

简要思路分析

题目要求:给定 6 个单词,识别出其中的英文数字(包括零到二十以及 a/both/another/first/second/third),将每个数字平方后对 100 取模,然后把这些两位数(或一位数)按照能组成最小整数的顺序拼接输出(去掉前导零),若无有效数字则输出 0。

核心策略:

  1. 建立从英文单词到“平方模 100 结果”的直接映射(例如 "one"→1, "two"→4, …),省去中间转换。
  2. 读入 6 个单词,匹配映射表,收集结果。
  3. 将收集到的数升序排序 —— 这样拼接得到的数字最小(贪心)。
  4. 输出时:第一个非零数直接输出(不补零),后续数强制输出两位(如 5 输出 05);如果所有数都是零,则输出一个 0。

完整代码

#include<bits/stdc++.h>
using namespace std;

char dic[30][20] = {
    "zero","one","two","three","four","five","six","seven","eight","nine","ten",
    "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
    "eighteen","nineteen","twenty","a","both","another","first","second","third"
};
int di[30] = {0,1,4,9,16,25,36,49,64,81,0,21,44,69,96,25,56,89,24,61,0,1,4,1,1,4,9};

int a[10], top = 0;
char s[100];

int main() {
    for(int i = 1; i <= 6; i++) {
        scanf("%s", s);
        for(int j = 0; j < 27; j++) {
            if(!strcmp(s, dic[j])) {
                a[++top] = di[j];
                break;
            }
        }
    }
    sort(a + 1, a + top + 1);

    bool flag = false;
    for(int i = 1; i <= top; i++) {
        if(flag) printf("%.2d", a[i]);
        else {
            if(a[i]) {
                printf("%d", a[i]);
                flag = true;
            }
        }
    }
    if(!flag) printf("0");
    return 0;
}

3.P1598 垂直柱状图

简要思路分析

输入四行文本(可能包含空格),只统计其中大写字母 A~Z 的出现次数。输出一个垂直柱状图:

关键点:

完整代码

#include <bits/stdc++.h>
using namespace std;

int main() {
    int cnt[26] = {0};
    string line;

    for (int i = 0; i < 4; ++i) {
        getline(cin, line);
        for (char ch : line)
            if (ch >= 'A' && ch <= 'Z')
                cnt[ch - 'A']++;
    }

    int maxh = 0;
    for (int i = 0; i < 26; ++i)
        if (cnt[i] > maxh) maxh = cnt[i];

    for (int h = maxh; h >= 1; --h) {
        for (int i = 0; i < 26; ++i) {
            if (cnt[i] >= h) cout << '*';
            else cout << ' ';
            if (i < 25) cout << ' ';
        }
        cout << endl;
    }

    for (int i = 0; i < 26; ++i) {
        cout << char('A' + i);
        if (i < 25) cout << ' ';
    }
    cout << endl;

    return 0;
}

4. P5734 【深基6.例6】文字处理软件

简要思路分析

用 string 类型的变量 doc 存储当前文档。根据操作类型分别处理:

注意:所有操作中的字符串均不含空格,可直接用 cin 读取;索引均从 0 开始,与题目描述一致。

完整代码

#include <bits/stdc++.h>
using namespace std;

int main() {
    int q;
    string doc;
    cin >> q >> doc;

    while (q--) {
        int op;
        cin >> op;
        if (op == 1) {
            string str;
            cin >> str;
            doc += str;          // 后接插入
            cout << doc << endl;
        }
        else if (op == 2) {
            int a, b;
            cin >> a >> b;
            doc = doc.substr(a, b);   // 截取 a 开始 b 个字符
            cout << doc << endl;
        }
        else if (op == 3) {
            int a;
            string str;
            cin >> a >> str;
            doc.insert(a, str);       // 在第 a 个字符前插入
            cout << doc << endl;
        }
        else if (op == 4) {
            string str;
            cin >> str;
            int pos = doc.find(str);
            if (pos == string::npos) cout << -1 << endl;
            else cout << pos << endl;
        }
    }
    return 0;
}

5. P1042 [NOIP 2003 普及组] 乒乓球

简要思路分析

读入所有比赛得分记录,直到遇到 E 为止,将所有 W 和 L 按顺序存入一个静态字符数组。然后分别模拟 11 分制 和 21 分制:

因为输入最多 2500 行,每行最多 25 个字符,加上可能的多余数据,静态数组开 100000 足够。

完整代码

#include <bits/stdc++.h>
using namespace std;

int main() {
    char record[100000];  // 静态数组存储比赛记录
    int len = 0;          // 有效记录长度
    char ch;
    while (cin >> ch && ch != 'E') {
        if (ch == 'W' || ch == 'L')
            record[len++] = ch;
    }

    // 处理 11 分制
    int a = 0, b = 0;
    for (int i = 0; i < len; i++) {
        if (record[i] == 'W') a++;
        else b++;
        if ((a >= 11 || b >= 11) && abs(a - b) >= 2) {
            cout << a << ":" << b << endl;
            a = b = 0;
        }
    }
    if (a != 0 || b != 0) cout << a << ":" << b << endl;
    cout << endl;

    // 处理 21 分制
    a = b = 0;
    for (int i = 0; i < len; i++) {
        if (record[i] == 'W') a++;
        else b++;
        if ((a >= 21 || b >= 21) && abs(a - b) >= 2) {
            cout << a << ":" << b << endl;
            a = b = 0;
        }
    }
    if (a != 0 || b != 0) cout << a << ":" << b << endl;

    return 0;
}

6.P2818 天使的起誓

简要思路分析

有 n 个盒子围成一圈,编号 1 到 n。从 1 号盒子开始顺时针方向数第 m 个盒子就是答案。因为 m 可以非常大(10^{1000} 级别),不能直接转为整数,必须用字符串处理。

核心公式:盒子编号 = (m-1) \bmod n + 1,等价于:

大数取模方法:
将 m 当作字符串,从高位到低位模拟:

r = 0

对于每一位数字 d:

r = (r \times 10 + d) \bmod n

最终 r 就是 m \bmod n。

注意 n 最大为 10^8,用 long long 或 int 均可(10^8 \times 10 < 2^{31})。

完整代码

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    string m;
    cin >> n >> m;

    long long r = 0;  // 存放余数
    for (char ch : m) {
        r = (r * 10 + (ch - '0')) % n;
    }

    if (r == 0) cout << n << endl;
    else cout << r << endl;

    return 0;
}