题解 | #密码强度等级#

密码强度等级

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

//一步一步写的
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main() {
    char str[301] = {0};
    int grade = 0;
    int c[4] = {0}; 
    scanf("%s", str);
    int len = strlen(str);
    if(len <= 4)
    {
        grade += 5;
    }
    else if(len >= 5 && len <= 7)
    {
        grade += 10;
    }
    else if(len >= 8)
    {
        grade += 25;
    }

    for(int i = 0; i<len; i++)
    {
        if(isupper(str[i]))
           c[0]++; //大写
        else if(isalpha(str[i]))
           c[1]++; //小写
        else if(str[i] >= '0' && str[i] <= '9')
           c[2]++; //数字
        else if((str[i] >= '!' && str[i] <= '@') || (str[i] >= '[' && str[i] <= '`') || (str[i] >= '{' && str[i] <= '~'))
            c[3]++; //符号;

    }

    if(c[0] != 0 && c[1] != 0)
        grade += 20;
    else if( c[0] != 0  || c[1] != 0 )
        grade += 10;
    if(c[2] == 1)
        grade += 10;
    else if(c[2] > 1)
        grade += 20;
    if(c[3] == 1)
        grade += 10;
    else if(c[3] > 1)
        grade += 25;
    if(c[0] != 0 && c[1] != 0 && c[2] != 0 && c[3] != 0)
        grade += 5;
    else if((c[0] != 0 || c[1] != 0) && c[2] != 0 && c[3] != 0 )
        grade += 3;
    else if((c[0] != 0 || c[1] != 0) && c[2] != 0 )
        grade += 2;
    
    if(grade >= 90)
        printf("VERY_SECURE\n");
    else if(grade >= 80)
        printf("SECURE\n");
    else if(grade >= 70)
        printf("VERY_STRONG\n");
    else if(grade >= 60)
        printf("STRONG\n");
    else if(grade >= 50)
        printf("AVERAGE\n");
    else if(grade >= 25)
        printf("WEAK\n");
    else if(grade >= 0)
        printf("VERY_WEAK\n");

    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务