1 条题解

  • 0
    @ 2026-10-23 18:34:05

    P76007 统一字母大小写 — 题解

    专题: 字符串
    适用题目: 新生 OJ 本地题包中的 P76007,不要直接用于外站同名题。

    解题思路

    先统计大小写字母的个数,再决定整串统一为大写或小写。

    复杂度

    时间 O(n)O(n),额外空间 O(1)O(1)。

    易错点

    大写与小写数量相等时,题目规定转小写。

    C11 参考程序

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <limits.h>
    
    int main(void) {
        static char s[100005]; scanf("%100004s", s);
        int up = 0, low = 0;
        for (int i = 0; s[i]; i++) {
            if (isupper((unsigned char)s[i])) up++;
            else low++;
        }
        for (int i = 0; s[i]; i++) s[i] = (char)(up>low ? toupper((unsigned char)s[i]) : tolower((unsigned char)s[i]));
        puts(s);
        return 0;
    }
    

    C++17 参考程序

    #include <bits/stdc++.h>
    using namespace std;
    using ll = long long;
    #define nl '\n'
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        string s;
        cin>>s;
        int up=0;
        for(char c:s)up+=!!isupper((unsigned char)c);
        bool upper=up>(int)s.size()-up;
        for(char &c:s)c=upper?toupper(c):tolower(c);
        cout<<s<<nl;
        return 0;
    }
    

    题解建议设置为训练结束后开放。普通判题无法验证是否实际使用指定函数或 STL。

    • 1

    信息

    ID
    1776
    时间
    2000ms
    内存
    256MiB
    难度
    1
    标签
    提交数
    1
    已通过
    1
    上传者