题解 | #密码验证合格程序#
密码验证合格程序
http://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
int main(void)
{
char str[101];
char a;
while(gets(str)){
bool sWord = true;
bool sword = true;
bool snum = true;
bool selse = true;
int stype = 0;
int len = strlen(str);
char sub[len-2][4];
int i, num;
if(len < 8)
goto NG;
//printf("%s %d ",str,len);
for(i=0, num=0; i<len; i++){
/* 分割为3个一组的字符串 */
if((len-i) >= 3){
memcpy(&sub[num], &str[i], 3);
sub[num][3] = '\0';
num++;
}
/*小写字母.数字.其它符号,以上四种至少三种 判断*/
if(str[i]>= 'A' && str[i]<= 'Z'){
if(sWord){
sWord = false;
stype++;
}
}else if(str[i]>= 'a' && str[i]<= 'z'){
if(sword){
sword = false;
stype++;
}
}else if(str[i]>= '0' && str[i]<= '9'){
if(snum){
snum = false;
stype++;
}
}else if(str[i] != ' ' && str[i]!= '\n'){
if(selse){
selse = false;
stype++;
}
}
//printf("%c %d\n",str[i], stype);
}
//printf("%d , num %d\n", stype, num);
if(stype < 3)
goto NG;
/* 长度大于2的包含公共元素的子串重复 判断 */
for(int i=0; i<num; i++){
//printf("%s\n", sub[i]);
for(int j=0; j<num; j++){
if(j == i)
continue;
if(strcmp(sub[i], sub[j]) == 0)
goto NG;
}
}
printf("OK\n");
continue;
NG:
printf("NG\n");
}
return 0;
}