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

来源:互联网 发布:c语言贪吃蛇源代码思路 编辑:程序博客网 时间:2024/05/29 15:26

描述: 

密码要求:

 

 

 

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;}



0 0
原创粉丝点击