原题目:
I - Problem I
统计每个元音字母在字符串中出现的次数。
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
题目分析:
看到题目的第一个想法就是,用字符数组接受输入的字符,并且用循环将一个个字母与元音对比,对比上则计数加一。总的思路也就是这样,但有很多细节问题需要注意。
比如数组的大小,输出的格式。我一直没找到的错误是在输入行数时要加一个getchar来接收回车。
AC代码如下:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char s[1000];
int n, i,j=0;
cin >> n;
getchar();
while (n--)
{
int n1 = 0, n2 = 0, n3 = 0, n4 = 0, n5 = 0;
gets(s);
for (i = 0; i < strlen(s); i++)
{
if (s[i] == 'a' || s[i] == 'A') n1++;
else if (s[i] == 'e' || s[i] == 'E') n2++;
else if (s[i] == 'i' || s[i] == 'I') n3++;
else if (s[i] == 'o' || s[i] == 'O') n4++;
else if (s[i] == 'u' || s[i] == 'U') n5++;
}
cout << "a:" << n1 << endl;
cout << "e:" << n2 << endl;
cout << "i:" << n3 << endl;
cout << "o:" << n4 << endl;
cout << "u:" << n5 << endl;
if (n != 0)
cout << endl;
}
return 0;
}