题目链接:1.音节判断 - 蓝桥云课 (lanqiao.cn)
解题思路:
如果第一项是元音,直接输出no,结束程序,如果满足第一项是辅音,就判断变化了几次,
如果当前字母是辅音且下一个字母是元音,就为一次变化
如果当前字母是元音且下一个字母是辅音,视为一次变化,
如果遍历完字符串,只变换了三次,就说明这个字符串的音节符合辅元辅元的结构,也就是count=3时输出yes,否则输出no
以下是c++代码:
#include <iostream>
using namespace std;
int main()
{
// 请在此输入您的代码
string s;
int count = 0;
std::cin >> s;
if ((s[0] == 'a' || s[0] == 'e' || s[0] == 'i' || s[0] == 'o' || s[0] == 'u') ) {
std::cout << "no";
return 0;
}
for (int i = 0; i < s.length() - 1; i++) {
if (!(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u') && (s[i + 1] == 'a' || s[i + 1] == 'e' || s[i + 1] == 'i' || s[i + 1] == 'o' || s[i + 1] == 'u')) {
count++;
}
if ((s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u') && !(s[i + 1] == 'a' || s[i + 1] == 'e' || s[i + 1] == 'i' || s[i + 1] == 'o' || s[i + 1] == 'u')) {
count++;
}
}
if (count == 3) {
std::cout << "yes";
}
else {
std::cout << "no";
}
return 0;
}
时过境迁,回头看下这题,优化了下代码,也是突然想到
#include <iostream>
#include<map>
using namespace std;
int main()
{
// 请在此输入您的代码
string s;
map<char,int> mp;
char str[5] = {'a','e','i','o','u'};
for(int i = 0;i < 5;i++){
mp[str[i]]++;
}
int count = 0;
cin >> s;
if(mp[s[0]]){
cout << "no";
return 0;
}
for(int i = 0;i < s.length() - 1;i++){
if(!mp[s[i]] && mp[s[i + 1]])count++;
if(mp[s[i]] && !mp[s[i + 1]])count++;
}
if(count == 3){
cout << "yes";
}else{
cout << "no";
}
return 0;
}