首页 > 试题广场 >

密码检查

[编程题]密码检查
  • 热度指数:17948 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

小明同学最近开发了一个网站,在用户注册账户的时候,需要设置账户的密码,为了加强账户的安全性,小明对密码强度有一定要求:

1. 密码只能由大写字母,小写字母,数字构成;

2. 密码不能以数字开头;

3. 密码中至少出现大写字母,小写字母和数字这三种字符类型中的两种;

4. 密码长度至少为8

现在小明受到了n个密码,他想请你写程序判断这些密码中哪些是合适的,哪些是不合法的。


输入描述:
输入一个数n,接下来有n(n≤100)行,每行一个字符串,表示一个密码,输入保证字符串中只出现大写字母,小写字母和数字,字符串长度不超过100。


输出描述:
输入n行,如果密码合法,输出YES,不合法输出NO
示例1

输入

1
CdKfIfsiBgohWsydFYlMVRrGUpMALbmygeXdNpTmWkfyiZIKPtiflcgppuR

输出

YES
使用的正则表达式 判断 比较好理解
import java.util.*;
public class test5 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int num =scanner.nextInt(); String [] strArr=new String[num]; for(int i=0;i<num;i++) { strArr[i]=scanner.next(); } for(int i=0;i<num;i++) { System.out.println(PanDu(strArr[i])); } } public static String PanDu(String str) { //至少为8 不能数字开头 只能由大写字母小写字母和数字构成 String regex="[a-zA-Z]{1}[a-zA-Z0-9]{7,}"; boolean flag= str.matches(regex); if(!flag) { return "NO"; } String result="NO"; //判断出现过几种类型 //判断第一个是什么类型  true 代表是 [a-z] false 代表是 [A-Z] boolean first=false; //判断第二个是否为 别的类型 true代表是  false 代表不是 boolean second=false; String firstStr=str.substring(0,1); if(firstStr.matches("[a-z]")) { first=true; } if(first) { for(int i=1;i<str.length()-1;i++) { String subStr=str.substring(i,i+1); second=subStr.matches("[A-Z0-9]"); //如果是第二个类型的 则就立刻退出循环 if(second) { result="YES"; break; } } }else { for(int i=1;i<str.length()-1;i++) { String subStr=str.substring(i,i+1); second=subStr.matches("[a-z0-9]"); //如果是第二个类型的 则就立刻退出循环 if(second) { result="YES"; break; } } } return result; } }

编辑于 2019-01-19 19:18:00 回复(1)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String[] arr = new String[n];
        for(int i=0;i<arr.length;i++){
            arr[i] = sc.next();
        }
        for(int i=0;i<arr.length;i++){
            System.out.println(panD(arr[i]));
        }
    }
    public static String panD(String str){
        if(str.length()<8){
            return "NO";
        }
        if(str.charAt(0)<='9'&&str.charAt(0)>='0'){
            return "NO";
        }
        int a=0,b=0,c=0;
        for(int i=0;i<str.length();i++){
            if(str.charAt(i)<'0'||(str.charAt(i)>'9'&str.charAt(i)<'A')||(str.charAt(i)>'Z'&str.charAt(i)<'a')||str.charAt(i)>'z'){
                return "NO";
            }
            if(str.charAt(i)<='9'&&str.charAt(i)>='0'){
                a = 1;
            }
            if(str.charAt(i)<='z'&&str.charAt(i)>='a'){
                b = 1;
            }
            if(str.charAt(i)<='Z'&&str.charAt(i)>='A'){
                c = 1;
            }
        }
        if(a+b+c>=2){
            return "YES";
        }else{
            return "NO";
        }
    }
}
发表于 2019-01-15 16:36:46 回复(3)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
//注意思考每个if()不满足的结果,就能明白为何要用goto跳到flag!!!!
int main()
{
    char arr[101] = { 0 };
    int n = 0, lg = 0, a = 0, b = 0, c = 0;
    
    flag://YES后重新进入
    while (scanf("%d", &n) != EOF)
    {
        scanf("%s", arr);
        if ( isalpha(arr[0]) != 0)//判断2条件
        {
            if ((lg = (int)strlen(arr)) >= 8)//判断4条件
            {
                while (lg >= 0)//进入1条件
                {
                    lg--;
                    if (isalpha(arr[lg]) || arr[lg] >= '0' && arr[lg] <= '9')//判断1条件
                    {
                        if (arr[lg] >= 'A' && arr[lg] <= 'Z')//判断3条件 大写
                            a = 1;
                        else if (arr[lg] >= 'a' && arr[lg] <= 'b')//小写
                            b = 1;
                        else//数字
                            c = 1;
                        if (a + b + c > 1)//3满足2即可
                        {
                            printf("YES\n");
                            goto flag;//跳出输出NO的循环
                        }
                    }
                    else
                        break;
                }

            }
        }
        printf("NO\n");
    }

    return 0;
}

发表于 2022-08-15 15:09:50 回复(0)
n=int(input())
num=['0','1','2','3','4','5','6','7','8','9']
for i in range(n):
    res='YES'
    s=input()
    if len(s)<8&nbs***bsp;s[0].isdigit():
        res='NO'
    else:
        has_digit, has_lower, has_upper = 0, 0, 0 # 必须有数字、大写、小写之中的两种  
        for char in s:     
            if char.isdigit():      
                has_digit = 1  
            elif char.islower():     
                has_lower = 1   
            elif char.isupper():   
                has_upper = 1    
            else: # 此时既不是数字也不是字母,肯定是特殊字符。     
                res='No'
                break
        if has_digit+has_lower+has_upper<2:
            res='No'
    print(res)


发表于 2020-06-12 16:44:11 回复(0)
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    string s;
    int shuzi=0,d=0,x=0;
    for(int i=0;i<n;i++)
    {
        cin>>s;
        if(s.size()<8)
        {
            cout<<"NO"<<endl;
            continue;
        }
        if(isdigit(s[0]))
        {
            cout<<"NO"<<endl;
            continue;
        }
        for(int j=0;j<s.size();j++)
        {
            if(isupper(s[j]))
                d++;
            if(islower(s[j]))
                x++;
            if(isdigit(s[j]))
                shuzi++;
        }
        if(x == sizeof(s) || d == sizeof(s) || shuzi == sizeof(s))
        {
            cout<<"NO"<<endl;
            continue;
        }
        cout<<"YES"<<endl;
    }
    return 0;
}

发表于 2019-06-28 16:29:27 回复(0)
必须输入一次就输出一次,我保存判断结果,一次性输出只有20%正确率。
发表于 2019-02-06 10:18:36回复(0)
参考了这位兄dei的想法,就对了😎😎😎😎😎
理由大概是,在自测那里尝试过一下,输入的数据不能超过2048,而这里给的样例68的那个例子,明显超出,所以不行,必须输入后直接输出,不能存储
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
bool judge(char* ch) {
    bool check = 0;
    if (strlen(ch)<8)
        return 0;
    int a[3] = { 0,0,0 };//分别记录0和9之间和大写小写的数目
    for (int i = 0; i<strlen(ch); i++) {
        if (ch[0] >= '0'&&ch[0] <= '9')
            return 0;
        if ((ch[i]<'A'&&ch[i]>'9') || ch[i]<'0' || (ch[i]>'Z'&&ch[i]<'a') || ch[i]>'z')
            return 0;
        if (ch[i] >= 'A'&&ch[i] <= 'Z')
            a[1]++;
        if (ch[i] >= '0'&&ch[i] <= '9')
            a[0]++;
        if (ch[i] >= 'a'&&ch[i] <= 'z')
            a[2]++;
    }
    int count = 0;
    for (int i = 0; i<3; i++) {
        if (a[i] > 0)
            count++;
    }
    if(count>=2) return 1;
    else return 0;
}
int main() {
    char ch[100];
    int n;
    while (cin >> n) {
        string str;
        for (int i = 0; i < n; i++){
            cin>>str;
            char ch[100];
            strcpy(ch, str.c_str());
            if (judge(ch)) cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
}

发表于 2019-02-11 19:57:48 回复(0)
#include<iostream>
using namespace std;
int main()
{
    int n,flagA,flagB,flagC;
    string psd;
    cin>>n;
    while(n--)
    {
        flagA=0;
        flagB=0;
        flagC=0;
        cin>>psd;
        if(psd.length()>=8&&isalpha(psd[0]))
        {
            for(auto c:psd)
            {
                if(isupper(c))    flagA=1;
                else if(islower(c))    flagB=1;
                else if(isdigit(c))    flagC=1;
                else    break;
            }
        }
        flagA+flagB+flagC<2 ? cout<<"NO"<<endl:cout<<"YES"<<endl;
    }
} 
设置3个flag分别表示密码有无大写,小写,数字,剩下的按题要求用库函数实现即可
发表于 2019-01-19 11:06:54 回复(2)
//长度和数字开头都简单,大小写和数字只有就一种不行的话就要借助字符比较ASCII码大小实现.
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int n=sc.nextInt();
            for(int x=0;x<n;x++){
                verify(sc.next());
            }
        }
    }
    public static void verify(String s){
        if(s.length()<8){
            System.out.println("NO");
            return;
        }else{
            char[] chr=s.toCharArray();
            if(chr[0]<10){
                System.out.println("NO");
            }else{
                int a=0;
                int b=0;
                int c=0;
                for(int x=0;x<chr.length;x++){
                    if(chr[x]<10){
                        a++;
                    }
                    if(chr[x]>64&&chr[x]<97){
                        b++;
                    }
                    if(chr[x]>96){
                        c++;
                    }
                }
                if(a+b==0){
                    System.out.println("NO");
                    return;
                }else if(b+c==0){
                    System.out.println("NO");
                    return;
                }else if(a+c==0){
                    System.out.println("NO");
                    return;
                }else{
                    System.out.println("YES");
                    return;
                }
            }
        }
    }
}

发表于 2019-01-11 00:04:34 回复(0)
int main()
{
	int n = 0;
	scanf("%d", &n);
	getchar();
	while (n--)
	{
		char arr[101] = { 0 };
		gets(arr);
		int sum1 = 0;
		int sum2 = 0;
		int sum3 = 0;
		if (strlen(arr) < 6)
		{
			printf("NO\n");
			continue;
		}
		if(arr[0] >= '0'&&arr[0] <= '9')
		{
			printf("NO\n");
		}
		for(int i= 0; i < strlen(arr); i++)
		{
			if (arr[i] <= 'z'&&arr[i] >= 'a' )
				sum1++;
			else if (arr[i] >= '0'&&arr[i] <= '9')
				sum2++;
			else if (arr[i] <= 'Z'&&arr[i] >= 'A')
				sum3++;
		}
		if (sum1 + sum2 +sum3== strlen(arr))
		{
			if (sum1 != 0 && sum2 != 0)
				printf("YES\n");
			if (sum2 != 0 && sum3 != 0)
				printf("YES\n");
			if (sum1 != 0 && sum3 != 0)
				printf("YES\n");
		}
		else
		{
			printf("NO\n");
		}
	}
	return 0;
}

发表于 2022-01-15 15:17:55 回复(0)

Python解法

def check_password(password):
    if len(password) < 8: # 判断长度条件
        return False
    if password[0] in "0123456789": # 判断是否以数字开头
        return False
    has_digit, has_lower, has_upper = 0, 0, 0 # 必须有数字、大写、小写之中的两种
    for char in password:
        if char.isdigit():
            has_digit = 1
        elif char.islower():
            has_lower = 1
        elif char.isupper():
            has_upper = 1
        else: # 此时既不是数字也不是字母,肯定是特殊字符。
            return False
    return has_digit + has_lower + has_upper >= 2

for _ in range(int(input())):
    print("YES" if check_password(input()) else "NO")
发表于 2019-02-24 14:07:24 回复(1)
//C++
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str;
    int num;
    cin >> num;
    while (num--)
    {
        cin >> str;
        bool flag1=0,flag2=0,flag3=0,flagerr=0;
        if ((str[0] >= '0'&&str[0] <= '9')|| str.length()<8)
            cout << "NO" << endl;
        else
        {
            for (char i : str)
            {
                if (i >= 'a'&&i <= 'z')
                    flag1 = 1;
                else if (i >= 'A'&&i <= 'Z')
                    flag2 = 1;
                else if (i >= '0'&&i <= '9')
                    flag3 = 1;
                else
                {
                    cout << "NO" << endl;
                    break;
                    flagerr = 1;
                }
            }
            if (flagerr == 1) continue;
            else
                cout << ((flag1 + flag2 + flag3 >= 2) ? "YES" : "NO")<<endl;
        }
    }
    return 0;
}
发表于 2019-03-16 20:52:48 回复(0)
#include<stdio.h>
#include <string.h>

int is_legal(char str[],int len)
{
    int count1=0;
    int count2=0;
    int i=0;
    
    if(len<8)
        return 0;
    
    if(str[0]<'9' && str[0]>'0')
        return 0;
    
    for(i=0;i<len-1;i++)
    {
        if( str[i]<'0'  ||  (str[i]>'9' && str[i]<'A') || str[i]>'z'  )
        {
            return 0;
        }     
        
        if( str[i] <'Z' && str[i]>'A')
         count1++;
        
        if(str[i] <'z' && str[i]>'a')
          count2++;

    }
        if(count1==len)
            return 0;
    
         if(count2==len)
            return 0;
    return 1;
}

int main()
{
    int n=0;
    scanf("%d",&n);
    char str[101];
    int len=0;
    int re=0;
    
    while(n)
    {
        scanf("%s",str);
        len= strlen(str);
        re= is_legal(str,len);
          if(re==0)
      printf("NO\n");
    else 
        printf("YES\n");
        getchar();
        n--;
    }
    
    
    return 0;    
}

发表于 2022-08-21 18:52:16 回复(0)
#include<stdio.h>
#include <string.h>
int main()
{
    int n=0;
    int i=0;
    char arr[100]={0};
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%s",&arr);
        int flag1=0;//数字
        int flag2=0;//小写字母
        int flag3=0;//大写字母
        int j=0;
        int len = strlen(arr);
        for(j=0;j<100;j++)
        {
            if(arr[j]>=48 && arr[j]<=57)//有数字
            {
                flag1=1;
            }
            else if(arr[j]>=65 && arr[j]<97)//有小写字母
            {
                flag2=1;
            }
            else//有大写字母
            {
                flag3=1;
            }
        }
        if(arr[0]>57 && len>=8 && (flag1+flag2+flag3>=2))
        {
           printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}

发表于 2022-01-17 12:38:12 回复(2)
#include <stdio.h>
#include<string.h>
int check(char* p)
{
    int  big = 0, sml = 0, sh = 0, err = 0;
    while (*p)
    {
        if (*p >= 'A' && *p <= 'Z')
            big=1;
        else if (*p >= 'a' && *p <= 'z')
            sml=1;
        else if (*p >= '0' && *p <= '9')
            sh=1;
        else
            err=1;
        p++;
    }
    if(err==0 && big+sml+sh>=2)
        return 1;
        else
         return 2;
}
int main()
{
    int n, i, j,m=0;
    scanf("%d", &n);
    getchar();
    char arr[n][100];
    for (i = 0; i < n; i++)
        gets(arr[i]);
    for (i = 0; i < n; i++)
    {
            if (arr[0][0] >= '0' && arr[0][0] <= '9')
            {
                printf("NO\n");
            }
            else  if (strlen(arr[i])>=8)
            {
                m = check(arr[i]);
                if (m == 2)
                    {
                        printf("NO\n");
                    }
                else
                {
                    printf("YES\n");
                    }
            }
            else  if (strlen(arr[i])<8){
            printf("NO\n");
            }
    }
    return 0;
}
编辑于 2024-01-23 21:10:16 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        while(n>0){
            String s=in.next();
            System.out.println(check(s)?"YES":"NO");
            n--; 
        }
    }

    public static boolean check(String s){
        if(s.length()<8) return false;
        boolean hasDigit,hasLetter;
        for(int i=0;i<s.length();i++){
            char ch=s.charAt(i);
            if(i==0&&Character.isDigit(ch)) return false;
            if(Character.isDigit(ch)) hasDigit=true;
            else if(Character.isLetter(ch)) hasLetter=true;
            else return false;
        }
        return true;
    }
}

发表于 2023-09-06 20:53:22 回复(0)
#include <stdio.h>
#include <string.h>

int main() {
    int n;
    scanf("%d",&n);
    while (n--) {
    char str[100];
    scanf("%s",str);
    int num=strlen(str);
    int a=0,b=0,c=0;
    if(str[0]<'A'&&(str[0]>'Z'&&str[0]<'a')&&str[0]>'z'){
        printf("NO\n");
        continue;
    }else if (num<8) {
    printf("NO\n");
    continue;
    }else{
    for (int i=1; i<num; i++) {
    if(str[i]<'0'&&(str[i]>'9'&&str[i]<'A')&&(str[i]>'Z'&&str[i]<'a')&&str[i]>'z'){
        printf("NO\n");
        break;
    }else if (str[i]>='0'&&str[i]<='0') {
    a=1;
    }else if (str[i]>='A'&&str[i]<='Z') {
    b=1;
    }else if (str[i]>='a'&&str[i]<'z') {
    c=1;
    }
    }
    if ((a+b+c)<2) {
    printf("NO\n");
    continue;
    }else {
    printf("YES\n");
    }
    }
    }
    return 0;
}
发表于 2023-09-04 23:21:25 回复(0)
#include <iostream>
#include <vector>
#include <regex>

bool validatePassword(const std::string& password) {
    if (password.length() < 8) {
        return false;
    }

    if (std::isdigit(password[0])) {
        return false;
    }

    int characterTypes = 0;
    if (std::regex_search(password, std::regex("[^A-Za-z0-9]"))) {
        return false;
    }
    if (std::regex_search(password, std::regex("[A-Z]"))) {
        characterTypes++;
    }
    if (std::regex_search(password, std::regex("[a-z]"))) {
        characterTypes++;
    }
    if (std::regex_search(password, std::regex("[0-9]"))) {
        characterTypes++;
    }

    return characterTypes >= 2;
}

int main() {
    int n;
    std::cin >> n;

    std::vector<std::string> passwords(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> passwords[i];
    }

    for (const std::string& password : passwords) {
        if (validatePassword(password)) {
            std::cout << "YES" << std::endl;
        } else {
            std::cout << "NO" << std::endl;
        }
    }

    return 0;
}

发表于 2023-08-14 19:30:22 回复(0)
#include <stdio.h>
#include<string.h>
#include<ctype.h>
int is_legal(char* arr,int len)
{
    int i = 0;
    int count1 =0;
    int count2 =0;
    if(len<8 ||arr[0]>='0'&&arr[0]<='9')
        return 0;
    for(i = 0;i<len-1;i++)
    {
        if(arr[i]<'0'||(arr[i]>'9' && arr[i]<'A')||arr[i]>'z')
            return 0;
        if(arr[i]>='0'&&arr[i]<='9')
            count1++;
        if(islower(arr[i]))//判断是否为小写字母
            count2++;
    }
    if(count1==len||count2==len)
        return 0;
    return 1;
}
int main() {
    int n = 0;
    scanf("%d", &n);
    int i = 0;
    char arr[101] = { 0 };
    int len = 0;
    int ret = 0;
    for (i = 1; i <= n; i++)
    {
        scanf(" %s",arr);
        len = strlen(arr);
        ret = is_legal(arr,len);
        if(ret)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

发表于 2023-08-13 17:23:08 回复(0)
#include <stdio.h>
#include<string.h>
 int n;
 char arr[101];
 scanf("%d",&n);
 for(int i =0;i<n;i++)
 {
    int flag=0,flag_1=0,flag_2=0;
    scanf("%s",arr,101);
    int sz = strlen(arr);
    if(arr[0]>='0'&&a[0]<='9'|| sz<8)
    {
        printf("NO\n");
        continue;
    }
    for(int j =0;j<sz;j++)
    {
        if(arr[j]>='a'&&arr[j]<='z')
        {
            flag=1;
        }
        else if(arr[j]>='0'&&arr[j]<='9')
        {
            flag_1=1;
        }
        else if(arr[j]>='A'&&arr[j]<='Z')
        {
            flag_2=1;
        }
        else
        {
            printf("NO\n");
            continue;
        }
    }
    if(flag+flag_1+flag_2>=2)
    {
        printf("YES\n");
        continue;
    }
 }
发表于 2023-01-24 15:59:57 回复(0)
#include <stdio.h>
#include <string.h>
int main()
{
    char password[100]={0};
    int n = 0;
    while(~(scanf("%d",&n)))
   {
     for(int i = 0;i<n;i++)
    {
        scanf("%s",password);
        if((strlen(password))<8)
        {
            printf("NO\n");
            continue;//密码长度至少为8
        }
        else if((password[i]>=0&&password[i]<=9)||(password[i]>='a'&&password[i]<='z')||(password[i]>='A'&&password[i]<='Z'))
        {
            if(password[0]>='0'&&password[0]<='9')
        {
            printf("NO\n");
            continue;
        }//密码不能以数字开头
        else
        {
            printf("YES\n");
            continue;
        }
        }
    }
   }
    return 0;
}

发表于 2023-01-09 20:55:42 回复(0)