1 条题解

  • 0
    @ 1977-1-31 7:29:30

    【例21.3】 字符类型判断

    解题思路

    依次检查 A~Z、a~z、0~9,分别输出 upper、lower、digit,其余 other。

    正确性说明

    三个范围互不重叠,分别覆盖三种指定字符类型;剩余统一为其他。

    易错点

    比较字符常量,例如 '0' 而非整数 0。

    复杂度

    输入规模固定,时间 O(1),额外空间 O(1)。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        char c;
        cin >> c;
        if (c >= 'A' && c <= 'Z')
            cout << "upper";
        else if (c >= 'a' && c <= 'z')
            cout << "lower";
        else if (c >= '0' && c <= '9')
            cout << "digit";
        else
            cout << "other";
        cout << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    143
    时间
    1000ms
    内存
    64MiB
    难度
    入门
    标签
    递交数
    0
    已通过
    0
    上传者