本文主要是介绍华为机试题:密码验证合格程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
描述: | 密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度超2的子串重复
说明:长度超过2的子串 |
#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <map>
#include <vector>using namespace std;int main()
{string str;bool isOk = true;while(cin >> str){isOk = true;/*判断长度是否符合要求*/if(str.size() <= 8){isOk = false;}/*包括大小写字母.数字.其它符号,以上四种至少三种*/if(isOk){int a[4] = {0};int i = 0, lens = str.size();for(i = 0; i < lens; i++){/*大写字母*/if (str[i] >= 'A' && str[i] <= 'Z'){a[0] = 1;}/*小写字母*/else if(str[i] >= 'a' && str[i] <= 'z'){a[1] = 1;}/*数字*/else if(str[i] >= '0' && str[i] <= '9'){a[2] = 1;}else{a[3] = 1;}}if(a[0] + a[1] + a[2] + a[3] < 3){isOk = false;}}/*不能有相同长度超2的子串重复*/if(isOk){int lens = str.size();int i = 0, j = 0;for(i = 0 ; i < lens - 3; i++){for(j = i + 2; j < lens; j++){if(str[j] == str[i] && str[j+1] == str[i+1]){isOk = false;break;}}}}if(isOk){cout << "OK" << endl;}else{cout << "NG" << endl;}str.clear();}return 0;
}
这篇关于华为机试题:密码验证合格程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!