满足条件:
1、字符数<8不行;
2、没有达到三种类型字符不行;
3、3个字符的子串有重复不行;
#include <iostream> using namespace std; string isVaild(string s) { if (s.length() < 8)return "NG"; int flag[4] = {0}; for (int i = 0; i < s.length(); i++) { if (s[i] >= 'A' && s[i] <= 'Z') flag[0] = 1; else if (s[i] >= 'a' && s[i] <= 'z') flag[1] = 1; else if (s[i] >= '0' && s[i] <= '9') flag[2] = 1; else if (s[i] != ' ' && s[i] != '\n') flag[3] = 1; } if (flag[0] + flag[1] + flag[2] + flag[3] < 3)return "NG"; for (int i = 0; i <= s.length() - 6; i++) { for (int j = i+3; j < s.length(); j++) { if (s.substr(i, 3) == s.substr(j, 3)) return "NG"; } } return "OK"; } int main() { string str; while (getline(cin, str)) { cout<<isVaild(str)<<endl; } return 0; }求助:为什么以下这种会一直报错“[Error] 'isValid' was not declared in this scope”。不懂!!
#include <iostream> using namespace std; bool isVaild(string s) { if (s.length() < 8)return false; int flag[4] = {0}; for (int i = 0; i < s.length(); i++) { if (s[i] >= 'A' && s[i] <= 'Z') flag[0] = 1; else if (s[i] >= 'a' && s[i] <= 'z') flag[1] = 1; else if (s[i] >= '0' && s[i] <= '9') flag[2] = 1; else if (s[i] != ' ' && s[i] != '\n') flag[3] = 1; } if (flag[0] + flag[1] + flag[2] + flag[3] < 3)return false; for (int i = 0; i < s.length() - 6; i ++) { for (int j = i+3; j < s.length(); j ++) { if (s.substr(i, 3) == s.substr(j, 3)) return false; else continue; } } return true; } int main() { string s; while(cin>>s){ if (isValid(s)) cout << "OK" << endl; else cout << "NG" << endl; } }