描述
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
数据范围:输入的字符串长度满足 1≤n≤100
输入描述:
一组字符串。
输出描述:
如果符合要求输出:OK,否则输出NG
示例1
输入:
021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000
输出:
OK NG NG OK
#include <stdio.h>
#include<string.h>
int main() {
/*
长度:>8
类型:大写字母+小写字母+数字+其他,至少3种
规则:长度大于2的子串不能重复出现
*/
int i=0,j=0;
char str[100];
while(scanf("%s\n",str)!=EOF){
/*
看 长度>8 ?
长度 <= 8:NG
长度 > 8 :看类型是否有3种及以上
类型 < 3种:NG
类型 >= 3种:看子串——只需要看长度等于3的子串,大于3以上的必有等于3的
*/
int f1=0,f2=0,f3=0,f4=0;//分别用于标识 大写字母、小写字母、数字、其他 是否已有
// 一看长度
if( strlen(str) <= 8) //长度不超过8,不合格
{
printf("NG\n");
}
else {
//统计类型数
for(i=0 ; i < strlen(str) ; i++)
{
if(str[i] >= 'A' && str[i] <= 'Z') //大写字母
{
f1=1;
}
else if(str[i] >= 'a' && str[i] <= 'z') //小写字母
{
f2=1;
}
else if(str[i] >= '0' && str[i] <= '9') //数字
{
f3=1;
}
else //其他:不包括空格和换行
{
//if(str[i]==' ' || str[i]=='\n') continue;
f4=1;
}
}
// 二看类型
if( (f1+f2+f3+f4) < 3 ) //类型小于3种,不合格
{
printf("NG\n");
}
else {
int f=0; //标识长度为3的子串是否重复出现
// 检查子串:只需看长度为3的子串,因为有长度大于3的子串必有长度为3的子串
for(i=0 ; i < strlen(str)-5 ; i++)
{
for(j=i+3 ; j < strlen(str)-2 ; j++)
{
if(str[i]==str[j] && str[i+1]==str[j+1] && str[i+2]==str[j+2])
{
f=1;
}
}
}
//三看子串
if(f==1) printf("NG\n"); //有重复子串
else printf("OK\n");
}
}
}
return 0;
}