[编程题]密码验证合格程序

来源:互联网 发布:python turtle库安装 编辑:程序博客网 时间:2024/06/17 10:11

Talk is cheap, show me the code.

一、问题描述

密码要求:

1.长度超过8位

2.包括大小写字母.数字.其它符号,以上四种至少三种

3.不能有相同长度超2的子串重复

说明:长度超过2的子串

输入描述:

一组或多组长度超过2的子符串。每组占一行

输出描述:

如果符合要求输出:OK,否则输出NG

输入例子:

021Abc9000021Abc9Abc1021ABC9000021$bc9000

输出例子:

OKNGNGOK

二、问题分析

判断大小写字母、数字、其他符号要么采用字符串find_first_of函数,要么进行一次循环遍历也可以统计出来。不能有相同长度超2的子串重复,其实就是说不能有长度为3的子串重复,这个可以通过字符串的find函数来实现,也可以通过遍历比较连续的3个字符来实现。

采用字符串的函数来实现。

#include <iostream>#include <string>#include <cctype>using namespace std;int main(){    string pd;    while (cin >> pd)    {        if (pd.size() <= 8)        {            cout << "NG" << endl;            continue;        } else {            int type = 0;            if (pd.find_first_of("0123456789") != string::npos)                type++;            if (pd.find_first_of("abcdefghijklmnopqrstuvwxyz") != string::npos)                type++;            if (pd.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") != string::npos)                type++;            for (int i = 0; i < pd.size(); i++)            {                if (!isalpha(pd[i]) && !isdigit(pd[i]))                {                    type++;                    break;                }            }            if (type < 3)            {                cout << "NG" << endl;                continue;            }            bool repeat = false;            for (int i = 0; i < pd.size() - 3; i++)            {                string temp = pd.substr(i + 3);                string::size_type k = temp.find(pd.substr(i, 3));                if (k != string::npos)                {                    cout << "NG" << endl;                    repeat = true;                    break;                }            }            if (!repeat)                cout << "OK" << endl;        }    }    return 0;}
0 0
原创粉丝点击