题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <stdio.h>
#include <string.h>
#define MAX 101
int second_isillegal(char str[],int n);
int third_isillegal(char str[],int n);
int main()
{
char str[MAX]={0};
int len;
while(scanf("%s",str) != EOF)
{
len=strlen(str);
if(len<8)
{
printf("NG\n");
}
else if(second_isillegal(str,len))
{
printf("NG\n");
}
else if(third_isillegal(str,len))
{
printf("NG\n");
}
else
{
printf("OK\n");
}
}
return 0;
}
int second_isillegal(char str[],int n)
{
int num[4]={0};
int sum =0;
int i;
//得到num是否大于等于3
for(i=0;i<n;i++)
{
if(str[i] >='0' && str[i]<='9'&& num[0]==0)
{
num[0]++;
}
else if(( (str[i]>=':' && str[i]<='@') || (str[i]>='[' && str[i]<='`') || (str[i]>='{' && str[i]<='~') || (str[i]>='!' && str[i]<='/') )&& (num[1]==0))
{
num[1]++;
}
else if(str[i]>='a' && str[i]<='z'&& num[2]==0)
{
num[2]++;
}
else if(str[i]>='A' && str[i]<='Z'&& num[3]==0)
{
num[3]++;
}
}
//成立返回1,不成立返回0
for(i=0;i<4;i++)
{
sum +=num[i];
}
if(sum >=3)
{
return 0;
}
else
{
return 1;
}
}
int third_isillegal(char str[],int n)
{
int i;
int j;
int num=0;
for (i=3;i<n-2;i++)
{
for(j=0;j<i;j++)
{
if(str[j]==str[i] && str[j+1]==str[i+1]&& str[j+2]==str[i+2])
{
num++;
}
}
}
if(num != 0)
{
return 1;
}
else
{
return 0;
}
}
#include <string.h>
#define MAX 101
int second_isillegal(char str[],int n);
int third_isillegal(char str[],int n);
int main()
{
char str[MAX]={0};
int len;
while(scanf("%s",str) != EOF)
{
len=strlen(str);
if(len<8)
{
printf("NG\n");
}
else if(second_isillegal(str,len))
{
printf("NG\n");
}
else if(third_isillegal(str,len))
{
printf("NG\n");
}
else
{
printf("OK\n");
}
}
return 0;
}
int second_isillegal(char str[],int n)
{
int num[4]={0};
int sum =0;
int i;
//得到num是否大于等于3
for(i=0;i<n;i++)
{
if(str[i] >='0' && str[i]<='9'&& num[0]==0)
{
num[0]++;
}
else if(( (str[i]>=':' && str[i]<='@') || (str[i]>='[' && str[i]<='`') || (str[i]>='{' && str[i]<='~') || (str[i]>='!' && str[i]<='/') )&& (num[1]==0))
{
num[1]++;
}
else if(str[i]>='a' && str[i]<='z'&& num[2]==0)
{
num[2]++;
}
else if(str[i]>='A' && str[i]<='Z'&& num[3]==0)
{
num[3]++;
}
}
//成立返回1,不成立返回0
for(i=0;i<4;i++)
{
sum +=num[i];
}
if(sum >=3)
{
return 0;
}
else
{
return 1;
}
}
int third_isillegal(char str[],int n)
{
int i;
int j;
int num=0;
for (i=3;i<n-2;i++)
{
for(j=0;j<i;j++)
{
if(str[j]==str[i] && str[j+1]==str[i+1]&& str[j+2]==str[i+2])
{
num++;
}
}
}
if(num != 0)
{
return 1;
}
else
{
return 0;
}
}