题目
密码验证合格程序_牛客题霸_牛客网 (nowcoder.com)
C语言
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
void fun_2024_6_16(void)
{
char str[128] = { 0 };
int len;
int a = 0, A = 0,d=0,_=0,sub=0;
while (scanf("%s", str) == 1)
{
//printf("%s", str);
len = strlen(str);
if (len <= 8)
{
printf("NG\n");
continue;
}
for(int i = 0; i < len; i++)
{
if (str[i] >= 'a' && str[i] <= 'z') a = 1;
else if (str[i] >= 'A' && str[i] <= 'Z')A = 1;
else if (str[i] >= '0' && str[i] <= '9')d = 1;
else _ = 1;
//判断是否有重复子串
if (i < len - 2)
{
char s[4] = {0};
s[0] = str[i];
s[1] = str[i + 1];
s[2] = str[i + 2];
if (strstr(str+i+3,s)!=NULL)
{
printf("NG\n");
sub = 1;
break;
}
}
}
if (a + A + d + _ < 3 && !sub)
{
printf("NG\n");
continue;
}
else if(a + A + d + _ >=3&&!sub)
printf("OK\n");
}
}
int main() {
fun_2024_6_16();
return 0;
}
C++
#include <iostream>
#include <string>
using namespace std;
void fun_2024_6_16(void) {
string str;
while (getline(cin, str)) {
//cout << str;
bool noSub = false;
int len = str.size();
if (len <= 8) {
cout << "NG" << endl;
continue;
}
for (int i = 0; i < len-6; i++) {
string subS;
subS = str.substr(i, 3);
if(int(str.find(subS,i+3))!=-1)
{
noSub = true;
break;
}
}
if (noSub) {
cout << "NG" << endl;
break;
}
int a = 0, A = 0, d = 0, _ = 0;
for (int i = 0; i < len; i++) {
if (str[i] >= 'a' && str[i] <= 'z') a = 1;
else if (str[i] >= 'A' && str[i] <= 'Z') A = 0;
else if (str[i] >= '0' && str[i] <= '9') d = 1;
else _ = 1;
}
if (a + A + d + _ < 3) cout << "NG" << endl;
else cout << "OK" << endl;
}
}
int main() {
fun_2024_6_16();
return 0;
}
Python
def fun_2024_6_16(s):
if len(s)<=8:
return 'NG'
a=0
A=0
d=0
_=0
for i,e in enumerate(s):
if s[i:i+3] in s[i+3:]: # 不用担心越界
return 'NG'
if e.islower():
a=1
elif e.isupper():
A=1
elif e.isdigit():
d=1
else:
_=1
if(a+A+d+_<3):
return 'NG'
return 'OK'
while True:
try:
s=input()
print(fun_2024_6_16(s))
except:
break
标签:str,16,每日,len,一道,算法,&&,include,void
From: https://blog.csdn.net/weixin_65816128/article/details/139716138