您的位置:首页 > 其它

华为机试题:密码验证合格程序

2016-02-02 16:28 330 查看
描述: 密码要求:

 

 

 

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: