求助!!!!

B3843 [GESP202306 三级] 密码合规

306djz @ 2024-07-21 09:11:47

零分

#include <bits/stdc++.h>
using namespace std;
int b,B,s,z,q,c = 1;//小写字母,大写字母,数字,字符,其他 
int main()
{
    char a[1001];
    for(int i = 0;i < 1001;i++)
    {
        cin >> a[i];
        if(a[i] >= 'a' && a[i] <= 'z')
        {
            b++;
        }
        else
        {
            q++;
        }
        if(a[i] >= 'A' && a[i] <= 'Z')
        {
            B++;
        }
        else
        {
            q++;
        }
        if(a[i] >= '0' && a[i] <= '9')
        {
            s++;
        }
        else
        {
            q++;
        }
        if(a[i] == '!' || a[i] == '@' || a[i] == '#' || a[i] == '$')
        {
            z++;
        }
        else
        {
            q++;
        }
        if(a[i] == ',')
        {
            if(b >= 2 && B >= 2 && s >= 2 && z >= 1)
            {
                if(z + s + B + b + q >= 6 && z + s + B + b + q <= 12)
                {
                    if(q > 0)
                    {
                        z = 0;
                        s = 0;
                        b = 0;
                        B = 0;
                        c = i + 1;
                        break;
                    }
                    for(int j = c;j < i;j++)
                    {
                        cout << a[j];
                    }
                    cout << endl;
                }
            }
            z = 0;
            s = 0;
            b = 0;
            B = 0;
            c = i + 1;
        }
    }
    return 0;
}

by Obijeb @ 2024-07-21 09:20:54

问一下你判断出当前密码有不合规字符的时候为什么要break

猜测你想要跳出判断语句,但break只能跳出循环语句


by 306djz @ 2024-07-21 09:23:59

@Obijeb 我就是想去输入下一个字符串


by Obijeb @ 2024-07-21 09:25:23

另,下面的形式在判断几种相互独立的并列情况时更有效,可读性更高


if(a[i]>='a' && a[i]<='z')
    b++;
else if(a[i]>='A' && a[i]<='Z')
    B++;
else if(a[i]>='0' && a[i]<='9')
    s++;
else if(a[i]=='!' || a[i]=='@' || a[i]=='#' || a[i]=='$')
    z++;
else
    q++;                            

by Obijeb @ 2024-07-21 09:26:49

@306djz break之后会跳出for循环,导致后面的字符串没有被处理


by 306djz @ 2024-07-22 09:03:34

@Obijeb 我不太会用else if()


by 306djz @ 2024-07-22 09:04:12

@Obijeb break不是进入下一个循环吗?


by Obijeb @ 2024-07-22 14:26:15

@306djz 请使用continue


by 306djz @ 2024-07-22 15:10:35

@Obijeb 我学过,但不会拼


|