题解 | #密码强度等级#

密码强度等级

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

package main

import (
    "fmt"
)

func CalculateScoreOfPassword(s string) int {
    var score int
    size := len(s)

    // 计算密码长度
    if size <= 4 {
        score += 5
    } else if 5 <= size && size <= 7 {
        score += 10
    } else {
        score += 25
    }

    // 判断字母情况
    var lowerLetter, upperLetter int
    for i:=0; i<size; i++ {
        if 'a' <= s[i] && s[i] <= 'z' {
            lowerLetter++
        } else if 'A' <= s[i] && s[i] <= 'Z' {
            upperLetter++
        }
    }

    if lowerLetter == 0 && upperLetter == 0 {
        score += 0
    } else if lowerLetter == 0 || upperLetter == 0 {
        score += 10
    } else {
        score += 20
    }

    // 判断数字个数
    var digitCount int
    for i:=0; i<size; i++ {
        if '0' <= s[i] && s[i] <= '9' {
            digitCount++
        }
    }
    if digitCount == 0 {
        score += 0
    } else if digitCount == 1 {
        score += 10
    } else {
        score += 20
    }

    // 判断符号个数
    var signCount int
    for i:=0; i<size; i++ {
        if !('0' <= s[i] && s[i] <= '9' || 'a' <= s[i] && s[i] <= 'z' || 'A' <= s[i] && s[i] <= 'Z') {
            signCount++
        }
    }
    if signCount == 0 {
        score += 0
    } else if signCount == 1 {
        score += 10
    } else {
        score += 25
    }

    // 计算奖励
    if upperLetter > 0 && lowerLetter > 0 && digitCount > 0 && signCount > 0 {
        score += 5
    } else if upperLetter + lowerLetter > 0 && digitCount > 0 && signCount > 0 {
        score += 3
    } else if upperLetter + lowerLetter > 1 && digitCount > 0 {
        score += 2
    }

    return score
}

func main() {
    var s string
    fmt.Scan(&s)
    score := CalculateScoreOfPassword(s)
    if score >= 90 {
        fmt.Println("VERY_SECURE")
    } else if score >= 80{
        fmt.Println("SECURE")
    } else if score >= 70 {
        fmt.Println("VERY_STRONG")
    } else if score >= 60 {
        fmt.Println("STRONG")
    } else if score >= 50 {
        fmt.Println("AVERAGE")
    } else if score >= 25 {
        fmt.Println("WEAK")
    } else {
        fmt.Println("VERY_WEAK")
    }
}
// 本题输入为一行字符串,所以采用:fmt.Scan(&s)

全部评论

相关推荐

天门一键开:她的意思是问你有没有论文吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务