HDU 2027统计元音

HDU 2027统计元音

题目:
统计每个元音字母在字符串中出现的次数。
Input
输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串。
Output
对于每个测试实例输出5行,格式如下:
a:num1
e:num2
i:num3
o:num4
u:num5
多个测试实例之间由一个空行隔开。

请特别注意:最后一块输出后面没有空行:)
Sample Input
2
aeiou
my name is ignatius
Sample Output
a:1
e:1
i:1
o:1
u:1

a:2
e:1
i:3
o:0
u:1

#include<iostream>
#include<cstdio>
#include<string>
#include<string.h>
using namespace std;

int main()
{
    int n,count,a,e,i,o,u;
    string str;
    scanf("%d",&n);
    getchar();
    while(n--)
    {
        getline(cin,str);
        a = 0;
        e = 0;
        i = 0;
        o = 0;
        u = 0;
        count = str.size();
        for(int j = 0;j < count;j++)
        {
            if(str[j] == 'a')
            {
                a++;
                continue;
            }
            else if(str[j] == 'e')
            {
                e++;
                continue;
            }
            else if(str[j] == 'i')
            {
                i++;
                continue;
            }
            else if(str[j] == 'o')
            {
                o++;
                continue;
            }
            else if(str[j] == 'u')
            {
                u++;
                continue;
            }
            else {continue;}
        }
        cout << "a:" << a << endl;
        cout << "e:" << e << endl;
        cout << "i:" << i << endl;
        cout << "o:" << o << endl;
        cout << "u:" << u << endl;
        if(n) cout << endl;
    }
    return 0;
}

- 注意事项:
这道题是一道hdu上的水题,需要注意的是输出的格式(注意题目中的:多个测试实例之间由一个空行隔开.请特别注意:最后一块输出后面没有空行:),也就是说除了最后一个数据之外,其他地方都应该在答案的后面再加一行空行。
另外,本人由于把getchar()写在了while循环内部导致一直wa…后来才发现只需要在输入第一个字符串之前把scanf输入时留下的回车键给吞掉就行了,如果写在while内会导致在输入之后的字符串时每次都把第一个字符吞掉.(这样写案例竟然也通过了…所以以后在提交前应多试几个例子)

运行结果:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41998938/article/details/83244717