题解 | #密码强度等级#

密码强度等级

https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361

#include <stdio.h>
#include <string.h>
char str[302];
int main() {
    int grade=0,lower=0,upper=0,digia=0,symbol=0,count=0;
    gets(str);
    int len = strlen(str);

    if(len<=4)
    {
        grade+=5;
    } else if(len>=8)
    {
        grade+=25;
    } else {
        grade +=10;
    }

    for(int i=0;i<len;i++)
    {
        if(str[i]>='a'&&str[i]<='z')
        {
            lower++;
        } else if (str[i]>='A'&&str[i]<='Z') {
            upper++;
        } else if(str[i]>='0'&&str[i]<='9'){
            digia++;
        } else if(str[i]>='!'&&str[i]<='/'){
            symbol++;
        } else if(str[i]>=':'&&str[i]<='@') {
            symbol++;
        } else if(str[i]>='['&&str[i]<='`') {
            symbol++;
        } else if(str[i]>='{'&&str[i]<='~'){
            symbol++;
        }
    }

    if((lower==0&&upper!=0)||(lower!=0&&upper==0))
    {
        grade+=10;
    } else if(lower!=0&&upper!=0)
    {
        grade+=20;
    }

    if(digia>1)
    {
        grade+=20;
    } else {
        if(digia==1)
        {
            grade+=10;
        }
    }

    if(symbol>1)
    {
        grade+=25;
    } else {
        if(symbol==1)
        {
            grade+=10;
        }
    }

    if(digia!=0&&upper!=0&&lower!=0&&symbol!=0)
    {
        count=5;
    } else if(digia!=0&&(upper!=0||lower!=0)&&symbol!=0)
    {
        count=3;
    }else if(digia!=0&&(upper!=0||lower!=0))
    {
        count=2;
    }
    
    grade+=count;
    if(grade>=90)
    {
        printf("VERY_SECURE");
    } else if(grade>=80&&grade<=90)
    {
        printf("SECURE");
    }else if(grade>=70&&grade<=80)
    {
        printf("VERY_STRONG");
    }else if(grade>=60&&grade<=70)
    {
        printf("STRONG");
    }else if(grade>=50&&grade<=60)
    {
        printf("AVERAGE");
    }
    else if(grade>=25&&grade<=50)
    {
        printf("WEAK");
    }else if(grade>=0&&grade<=25)
    {
        printf("VERY_WEAK");
    }
    // printf("%d",grade);
    return 0;
}

使用if完成,值得注意的是检查字符串的元素种类时,if应该先检查元素最多的情况,不然会执行错误

全部评论

相关推荐

与火:这不接? 留子的钱不挣白不挣
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务