先贴上在VC6.0上运行无误但在HDOJ后台海量测试数据通之下无法通过的代码,以便以后分析错误原因。
#include <iostream> using namespace std; bool isVowel(char letter) { switch(letter) { case 'a':case 'e':case 'i':case 'o':case 'u': return true; default: return false; } } bool isSame(char ch_1,char ch_2) { if(ch_1 == ch_2 && (ch_1 != 'e' && ch_1 != 'o')) return true; else return false; } bool allSame(char ch_1,char ch_2,char ch_3) { if( isVowel(ch_1) && isVowel(ch_2) && isVowel(ch_3) \ || ((!isVowel(ch_1)) && (!isVowel(ch_2)) && (!isVowel(ch_3)) )) return true; else return false; } bool isAcceptable(char *pwd) { char *pBegin,*pCur,*pNext; int vNum; int len = strlen(pwd); if(len < 3) { if(len == 1 && isVowel((char)pwd[0])) return true; if(len == 2 && isSame(pwd[0],pwd[1])) return true; else return false; } else { pBegin = pwd; pCur = pwd + 1; pNext = pwd + 2; char *pEnd = pwd + len;//最后一个字符串的下一位为结束点 vNum = 0; while(pNext != pEnd) { if(isVowel(*pBegin)) ++vNum; if(allSame(*pBegin,*pCur,*pNext))//三个连续的字母 return false; if(isSame(*pBegin,*pCur) || isSame(*pCur,*pNext)){ //两个连续的字母相同 return false; } else{ pBegin++; pCur++; pNext++; } } if(vNum == 0) //一个元音都没有 return false; else return true; } } int main() { char pwd[1000]; while(cin >> pwd && (strcmp(pwd,"end") != 0)) { if(isAcceptable(pwd)) { cout << "<" << pwd << "> is acceptable." << endl; } else { cout << "<" << pwd << "> is not acceptable." << endl; } } }
版本2:更为简洁,少了非常多的函数调用开销,效率较之前版本有所提升。不过仍旧不能通过OJ的测试。
#include <iostream> using namespace std; bool isVowel(char letter) { switch(letter) { case 'a':case 'e':case 'i':case 'o':case 'u': return true; default: return false; } } bool notVowel(char ch) { if(!isVowel(ch)) return true; else return false; } bool isAcceptable(char *pwd) { int len = strlen(pwd); int n_vowel = 0; if(1 == len && isVowel(pwd[0])) return true; for(int i = 1; i < len; ++i) { if(isVowel(pwd[i])) ++n_vowel; if(pwd[i-1] == pwd[i] && (pwd[i-1] != 'e') && (pwd[i-1] != 'o')) //两个连续并且相同的字母 return false; if(isVowel(pwd[i-1]) && isVowel(pwd[i]) && isVowel(pwd[i+1])) //三个全是元音 return false; if(notVowel(pwd[i-1]) && notVowel(pwd[i]) && notVowel(pwd[i+1])) //三个全是辅音 return false; } if(!n_vowel) return false; else return true; } int main() { char pwd[1000]; while(cin >> pwd && (strcmp(pwd,"end") != 0)) { if(isAcceptable(pwd)) { cout << "<" << pwd << "> is acceptable." << endl; } else { cout << "<" << pwd << "> is not acceptable." << endl; } } }
网友答案:
5440699 | 2012-03-01 10:48:22 | Accepted | 1039 | 15MS | 256K | 936 B | C++ | ajioy |
#include <iostream> #include <string> using namespace std; int isVowel(char *pwd,int i) {return (pwd[i] == 'a' || pwd[i] == 'e' || pwd[i] == 'i' || pwd[i] == 'o' || pwd[i] == 'u');} int main() { char pwd[1000]; char str[2][30] = {"not acceptable.","acceptable."}; while(cin >> pwd && ( strcmp(pwd,"end") != 0)) { int len = strlen(pwd); int flag1=0,flag2=1,flag3=1; for(int i = 0; i < len; ++i) { if(isVowel(pwd,i)) { flag1 = 1; if(i < len - 2 && isVowel(pwd,i+1) && isVowel(pwd,i+2) ) flag2 = 0; if(i < len - 1 && pwd[i] == pwd[i+1] && (pwd[i] != 'e' && pwd[i] != 'o')) flag3 = 0; } else { if(i < len - 2 && !isVowel(pwd,i+1) && !isVowel(pwd,i+2)) flag2 = 0; if(i < len - 1 && pwd[i] == pwd[i+1]) flag3 = 0; } } cout << "<" << pwd <<"> is " << str[flag1 && flag2 && flag3] << endl; } }