题目:
Problem Description
One potential solution is to generate "pronounceable" passwords that are relatively secure but still easy to remember.
FnordCom is developing such a password generator. You work in the quality control department, and it's your job to test the generator and make sure that the passwords are acceptable. To be acceptable, a password must satisfy these three rules:
It must contain at least one vowel.
It cannot contain three consecutive vowels or three consecutive consonants.
It cannot contain two consecutive occurrences of the same letter, except for 'ee' or 'oo'.
(For the purposes of this problem, the vowels are 'a', 'e', 'i', 'o', and 'u'; all other letters are consonants.) Note that these rules are not perfect; there are many common/pronounceable words that are not acceptable.
Input
The input consists of one or more potential passwords, one per line, followed by a line containing only the word 'end' that signals the end of the file. Each password is at least one and at most twenty letters long and consists only of lowercase letters.
Output
For each password, output whether or not it is acceptable, using the precise format shown in the example.
Sample Input
a
tv
ptoui
bontres
zoggax
wiinq
eep
houctuh
end
Sample Output
<a> is acceptable.
<tv> is not acceptable.
<ptoui> is not acceptable.
<bontres> is not acceptable.
<zoggax> is not acceptable.
<wiinq> is not acceptable.
<eep> is acceptable.
<houctuh> is acceptable.
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1039
解题:
我考虑到的问题有:(满足的三个规则)
It must contain at least one vowel.
It cannot contain three consecutive vowels or three consecutive consonants.
It cannot contain two consecutive occurrences of the same letter, except for 'ee' or 'oo'.
1、对字符串的判断有很多规则,怎样合理来处理每一个满足的规则;
2、连续多个元音、辅音如何来判断;
3、字符串输入输出细节问题;
考虑到判断字符串合法的条件有3个,所以用三个子函数分别来判断,因为只需要知道是否符合条件,所以用bool类型的函数,返回true/false,在主函数中用if判断是否三个条件都是true,即可。 连续元辅音判断,用for对数组从头到尾判断,num来计数,再通过num判断。
代码:
#include<cstdio>
#include<cstring>
char x[30];
bool f1(int num)
{
int flag=0;
for(int i=0;i<num;i++)
{
if(x[i]=='a'||x[i]=='e'||x[i]=='i'||x[i]=='o'||x[i]=='u')
{
flag=1;
break;
}
}
if(flag==0)
return false;
else
return true;
}
bool f2(int num)
{
int flag_y=0;
int flag_f=0;
for(int i=0;i<num &&flag_y<3&&flag_f<3;i++)
{
if(x[i]=='a'||x[i]=='e'||x[i]=='i'||x[i]=='o'||x[i]=='u')
{
flag_y++;
flag_f=0;
}
else
{
flag_f++;
flag_y=0;
}
}
if(flag_y==3||flag_f==3)
return false;
else
return true;
}
bool f3(int num)
{
int flag=1;
for(int i=0; i<num-1 && flag==1 ;i++)
{
if(x[i]==x[i+1]&&x[i]!='e'&&x[i]!='o')
flag=0;
}
if(flag==0)
return false;
else
return true;
}
int main()
{
while(scanf("%s",x)!=EOF&&strcmp(x,"end")!=0)
{
int num=strlen(x);
if(f1(num)&&f2(num)&&f3(num))
printf("<%s> is acceptable.\n",x);
else
printf("<%s> is not acceptable.\n",x);
}
}