题解 | #密码验证合格程序#

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

#include <iostream>
#include <string>
#include <vector>
using namespace std;

//三个限制 那就设置三个步骤即可
bool fun1(string str)
{
	//长度必须超过八位
	if (str.length() <= 8)
	{
		return false;
	}
	//要有大小写字母,数字,其他字符至少三种
	//可不可以用ascII码来进行区分
	int count = 0; //设置一个数字标志位 用来确定是否已经使用了数字
	int Icount = 0;//大写标志位
	int icount = 0;//小写
	int xcount = 0;//其他标志位
	for (int i = 0; i < str.length(); i++)
	{ 
		//把标志位判断放到外面的if 可能会导致其他字符那里出现问题
		//为数字
		if ((*(str.substr(i,1).data())>='0') 
			&& (*(str.substr(i, 1).data()) <= '9'))
		{
			if (count == 0)
			{
				count=1;
			}
			continue;
		}
		//为大写字母
		else if ((*(str.substr(i, 1).data()) >= 'A')
			&& (*(str.substr(i,  1).data()) <= 'Z'))
		{
			if (Icount == 0)
			{
				Icount=1;
			}
			continue;
		}
		//为小写字母
		else if ((*(str.substr(i,  1).data()) >= 'a')
			&& (*(str.substr(i, 1).data()) <= 'z'))
		{
			if (icount == 0)
			{
				icount = 1;
			}
			continue;
		}
		//为其他字符  
		else if ((*(str.substr(i, 1).data()) != ' ')
			&& (*(str.substr(i, 1).data()) != '\n')
			&& xcount == 0)
		{
			xcount = 1;
			continue;
		}
		//判断是否已经满足条件了 不需要继续往后判断了
		if ((count + icount + Icount + xcount) >= 3)
		{
			break;
		}
	}
	if ((count + icount + Icount + xcount) < 3)
	{
		return false;
	}
	//不能有长度大于2的包含公共元素的子串重复 不能有3以及以上的
	//遍历
	string strdata;
	for (int i = 0; i < str.length()-3; i++)
	{
		strdata = str.substr(i, 3);
		for (int j = i+1; j < str.length()-3; j++)
		{
			if (strdata == str.substr(j,3))
			{
				return false;
			}
		}
	}
	return true;

}
int main()
{
	string str;
	while (cin >> str)
	{
		bool iftrue = fun1(str);
		if (iftrue)
		{
			cout << "OK" << endl;
		}
		else
		{
			cout << "NG" << endl;
		}


	}
	system("pause");
	return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务