华为机试-密码验证合格程序(HJ20)-纯C
密码验证合格程序
http://www.nowcoder.com/questionTerminal/184edec193864f0985ad2684fbc86841
纯C
#include <stdlib.h> #include <stdio.h> void judge_char(char c, int *flag) { if(c >= 'A' && c <= 'Z') flag[0]=1; else if(c >= 'a' && c <= 'z') flag[1]=1; else if(c >= '0' && c <= '9') flag[2]=1; else flag[3]=1; } int main() { char * str = (char *)malloc(sizeof(char )*50); while(gets(str)) { char *pstr=str; int len=strlen(str), sum=0; if(len<=8) { printf("NG\n"); continue; } int count[4] = {0}; int flag=0; //用于判断是否终止循环 for(int i=0; i<(len-4); i++) { for(int j=i+1; j<(len-3); j++) { if(strncmp(pstr+i, pstr+j, 3) == 0) { flag=1; printf("NG\n"); break; } } if(flag) break; judge_char(str[i], count); } if(flag) continue; //再加上后面的4个字符判断 for(int i=len-4; i<len; i++) { judge_char(str[i], count); } for(int i=0; i<4; i++) { sum+=count[i]; } if(sum >=3) printf("OK\n"); else printf("NG\n"); } free(str); return 0; }