题解 | 密码验证合格程序
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
#include <stdbool.h>
#include <stdio.h>
#include<string.h>
int vary(char*);
int same(char*, int);
char arr[100];
int main(){
while(scanf("%s", arr) != EOF){
int len = strlen(arr);
if(len<9){
printf("NG\n");
}
else{
if(vary(arr) == false){
printf("NG\n");
}
else{
if(same(arr, len) == false){
printf("NG\n");
}
else{
printf("OK\n");
}
}
}
}
}
int vary(char* parr){ //判断多样性
int big, small, num, other;
big = small= num= other= 0;
while(*(parr) != '\0'){
if(*parr>='A' && *parr<='Z'){
big = 1;
}
else if(*parr>='a' && *parr<='z'){
small = 1;
}
else if(*parr>='0' && *parr<='9'){
num = 1;
}
else{
other = 1;
}
parr++;
}
if(big+small+num+other >2){
return true;
}
else {
return false;
}
}
int same(char* parr, int len){ //判断是否有相同子串
if(len<6){
return true;
}
for(int i=0;i<len-5;i++) {
for (int j = i + 3; j < len - 2; j++) {
if (strncmp(parr + i, parr + j, 3) == 0) {
return false;
}
}
}
return true;
}
查看19道真题和解析