题解 UVA492 【Pig-Latin】
这题目的测试点可能有点坑
我几次样例过了,但总是WA
一开始没用大家的getline方法,导致最后弄了半天还是WA
代码,麻烦帮我看看哪错了,可以发在评论区:
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
bool pd(char c)
{
return (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U');
}
int main()
{
string s;
while(cin >> s)
{
bool f = false;
int len = s.length() - 1;
if(s[len] == '.') f = true;
if(pd(s[0]))
{
if(f)
{
cout << s.substr(0, len) << "ay.";
}
else
{
cout << s << "ay";
}
}
else
{
for(int i = 1; i <= (f ? len - 1 : len); i++)
{
cout << s[i];
}
cout << s.front() << "ay";
if(f)
{
cout << ".";
}
}
char b = getchar();
cout << b;
}
return 0;
}
后来看了看题解的代码,发现都是用getline,索性也用getline,然后就过了……
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
inline bool judge(const char& c)
{
return (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U');
}
int main()
{
string s;
ios::sync_with_stdio(false);
while(getline(cin, s))
{
int len = s.length() - 1;
string tmp = "";
for(register int i = 0; i <= len; i++)
{
bool f = true;
if(isalpha(s[i]))
{
char now = s[i];
if(judge(s[i]))
{
cout << s[i];
f = false;
}
while(isalpha(s[++i])) cout << s[i];
if(f)
{
cout << now;
}
i--;
cout << "ay";
}
else
{
cout << s[i];
}
}
cout << "\n";
}
return 0;
}

浙公网安备 33010602011771号