1 条题解

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

    P76008 三行元音计数 — 题解

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

    解题思路

    使用逐行读取,分别数三个句子中的元音,检验计数是否依次为 5、7、5。

    复杂度

    时间 O(L)O(L),额外空间 O(L)O(L)。

    易错点

    不能使用 %s 或 cin >> s 读取含空格的整行。

    C11 参考程序

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <limits.h>
    
    int main(void) {
        char s[205]; int need[3] = {5,7,5}, ok = 1;
        for (int k = 0; k < 3; k++) {
            if (!fgets(s, sizeof s, stdin)) return 0;
            int cnt = 0;
            for (int i = 0; s[i]; i++) {
                char c = (char)tolower((unsigned char)s[i]);
                if (strchr("aeiou", c)) cnt++;
            }
            if (cnt != need[k]) ok = 0;
        }
        puts(ok ? "YES" : "NO");
        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;
        int want[3]={
            5,7,5
        }
        ;
        bool ok=true;
        for(int i=0;i<3;i++){
            getline(cin,s);
            int cnt=0;
            for(char c:s){
                c=tolower(c);
                if(string("aeiou").find(c)!=string::npos)cnt++;
            }
            if(cnt!=want[i])ok=false;
        }
        cout<<(ok?"YES":"NO")<<nl;
        return 0;
    }
    

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

    • 1

    信息

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