题解 | #密码验证合格程序#
密码验证合格程序
http://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <stdio.h>
static int judgeType(char *str, int len)
{
if(!str || len <= 0)
{
return -1;
}
int strType[4] = {0};
for(int i = 0; i < len; i++)
{
if(str[i] >= 'a' && str[i] <= 'z')
{
strType[0] = 1;
}
else if(str[i] >= 'A' && str[i] <= 'Z')
{
strType[1] = 1;
}
else if(str[i] >= '0' && str[i] <= '9')
{
strType[2] = 1;
}
else
{
strType[3] = 1;
}
}
if(strType[0] + strType[1] + strType[2] + strType[3] < 3)
{
return -1;
}
return 0;
}
static int checkRepetition(char *str) //快慢指针循环判断子串是否重复
{
if(!str)
{
return -1;
}
int fast = 1; //快指针
int slow = 0; //慢指针
int count = 0; //相同子串计数
while(str[slow] != '\0')
{
if(str[fast++] == str[slow]) //快指针开始和数组首位比较
{
for(int i = 0; i < 3; i++)
{
if(str[fast+i-1] == '\0')//说明到结尾了,str[fast+i-1]表示慢指针
{
break;
}
else if(str[fast+i-1] == str[slow+i])
{
count++;
}
}
if(count >= 3)
{
return -1; //说明子串重复大于3个
}
else
{
count = 0;
}
}
else if(str[fast] == '\0') //说明一轮比较结束
{
slow++;
fast = slow+1;
}
}
return 0;
}
int main()
{
char str[100] = {0};
while(scanf("%s", str) != EOF)
{
int len = strlen(str);
if(len < 9) //1.长度超过8位
{
printf("NG\n");
}
else
{
if(judgeType(str, len) < 0) //2.包括大小写字母.数字.其它符号,以上四种至少三种
{
printf("NG\n");
}
else
{
if(checkRepetition(str) < 0)
{
printf("NG\n");
}
else
{
printf("OK\n");
}
}
}
}
return 0;
}