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