poj 1575 Easier Done Than Said?【字符串处理】
题意:给你一段密码,判断这段密码是否安全,若安全则是acceptable。
密码必须同时满足下面三个限制条件才是安全密码:1、必须包含元音字母;
2、不能包含连续的三个元音或辅音字母;
3、除了ee和oo外,任何两个连续字母如果相同,密码就不安全。
总结一下,很久没编程了,通过这道题学会或者温习了以下知识点:
1.scanf("%s",pwd);和gets(pwd);的区别
2.字符串的函数间传值
3.char vowel[]="aeiou";和char vowel[]={'a','e','i','o','u','\0'};
字符串赋初值的两种方法
AC的代码:
#include <stdio.h>
#include <string.h>
bool thereIsAVowel(char *pwd)//返回ture代表合格
{
//char vowel[]={'a','e','i','o','u','\0'};
char vowel[]="aeiou";
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<strlen(pwd);j++)
{
if(pwd[j]==vowel[i])
return true;
}
}
return false;
}
bool threeVowel(char *pwd)
{
int vowel=0;//初始值为0,如果连续不为元音-1,连续为元音+1,交替出现清0
char preletter='?';//记录前一个字符
int i;
for(i=0;i<strlen(pwd);i++)
{
if(pwd[i]==preletter && (pwd[i]!='e' && pwd[i]!='o'))
//构成连续2个相同
return false;
//下面判断连续3个元音或辅音
if(pwd[i]=='a' || pwd[i]=='e' || pwd[i]=='i' || pwd[i]=='o' || pwd[i]=='u')
{
if(vowel<0)//前面正在记录辅音
vowel=1;
else
vowel++;
}else{//pwd[i]为辅音时
if(vowel>0)
vowel=-1;
else
vowel--;
}
if(vowel>=3 || vowel<=-3)
return false;
preletter=pwd[i];
}
return true;
}
int main()
{
char pwd[30];
while(scanf("%s",pwd)!=EOF) //"%s"不会输入空格
//while(gets(pwd)) //gets会输入空格
{
if(strcmp(pwd,"end")==0) break;
//1.必须含有元音字母
if(!thereIsAVowel(pwd))
{
printf("<%s> is not acceptable.\n",pwd);
continue;
}
//3个连续的辅音或元音,2个相同的可以一起解决了
if(!threeVowel(pwd))
{
printf("<%s> is not acceptable.\n",pwd);
continue;
}
//可用密码
printf("<%s> is acceptable.\n",pwd);
}
return 0;
}
最后更新:2017-04-03 12:56:20