I - Problem I HDU - 2027
ac代码,顺便提提getchar()的用法,重要的是格式输入,如何合理输入,空格与回车的区分。
getchar()是到缓冲区读取第一个字符
首先,从键盘输入许多字符,最后回车后,所有字符包括回车放到缓冲区
这时才开始循环执行getchar(),读入一个字符判断是否回车,不是就继续读,是回车就退出循环(之所以说是顺序接受一行字符,是因为它取的始终是缓冲区中第一个字符,每取一个缓冲区的字符少一个,也就是顺序读取了)
/*
miku saiko
*/
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
///int m = 1, x = 0;
getchar();
while (n)
{
char s[105] = {0};
int a = 0, e = 0, i = 0, o = 0, u = 0;
gets_s(s, 100);
for (int k = 0; k < 100; k++)
{
if (s[k] == 'a') { a++; }
if (s[k] == 'e') { e++; }
if (s[k] == 'i') { i++; }
if (s[k] == 'o') { o++; }
if (s[k] == 'u') { u++; }
}
cout << "a:" << a << endl;
cout << "e:" << e << endl;
cout << "i:" << i << endl;
cout << "o:" << o << endl;
cout << "u:" << u << endl;
n--;
if (n)
{
cout << endl;
}
}
return 0;
}
之前错误的代码(错在了把s[105]放在了外面)
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
getchar();
///int m = 1, x = 0;
char s[105];
while (n)
{
int a = 0, e = 0, i = 0, o = 0, u = 0;
gets_s(s, 100);
for (int k = 0; k < 100; k++)
{
if (s[k] == 'a') { a++; }
if (s[k] == 'e') { e++; }
if (s[k] == 'i') { i++; }
if (s[k] == 'o') { o++; }
if (s[k] == 'u') { u++; }
}
cout << "a:" << a << endl;
cout << "e:" << e << endl;
cout << "i:" << i << endl;
cout << "o:" << o << endl;
cout << "u:" << u << endl;
n--;
if (n)
{
cout << endl;
}
}
return 0;
}