HJ87. 密码强度等级
描述
密码按如下规则进行计分,并根据不同的得分为密码进行安全等级划分。输入描述
输入一个string的密码
输出描述
输出密码等级
示例1
输入:
38$@NoNoN
输出:
VERY_SECURE
说明:
样例的密码长度大于等于8个字符,得25分;大小写字母都有所以得20分;有两个数字,所以得20分;包含大于1符号,所以得25分;由于该密码包含大小写字母、数字和符号,所以奖励部分得5分,经统计得该密码的密码强度为25+20+20+25+5=95分。示例2
输入:
Jl)M:+
输出:
AVERAGE
说明:
示例2的密码强度为10+20+0+25+0=55分。C 解法, 执行用时: 1ms, 内存消耗: 272KB, 提交时间: 2020-07-07
#include<stdio.h> #include<string.h> int lenScore(char password[], int passwordLength) { if (4 >= passwordLength) return 5; else if (4 < passwordLength && 8 > passwordLength) return 10; else if (8 <= passwordLength) return 25; } int alphaScore(char password[], int passwordLength) { int lowercase = 0, capital = 0; for(int i = 0; i < passwordLength; i++) { if('a' <= password[i] && 'z' >= password[i]) lowercase++; if('A' <= password[i] && 'Z' >= password[i]) capital++; } if (0 == lowercase && 0 == capital) return 0; else if(0 < lowercase && 0 < capital) return 20; else if(0 < lowercase || 0 < capital) return 10; } int digitScore(char password[], int passwordLength) { int digit = 0; for(int i = 0; i < passwordLength; i++) { if('0' <= password[i] && '9' >= password[i]) digit++; } if (0 == digit) return 0; else if(1 == digit) return 10; else if (1 < digit) return 20; } int symbolScore(char password[], int passwordLength) { int symbol = 0; for(int i = 0; i < passwordLength; i ++) { if(('!' <= password[i] && '/' >= password[i]) || (':' <= password[i] && '@' >= password[i]) || ('[' <= password[i] && '`' >= password[i]) || ('{' <= password[i] && '~' >= password[i])) symbol++; } if(0 == symbol) return 0; else if(1 == symbol) return 10; else if(1 < symbol) return 25; } int extraScore(char password[], int passwordLength) { int alpha = alphaScore(password, passwordLength); int digit = digitScore(password, passwordLength); int symbol = symbolScore(password, passwordLength); if(10 < alpha && 0 < digit && 0 < symbol) return 5; else if(0 < alpha && 0 < digit && 0 < symbol) return 3; else if(0 < alpha && 0 < digit && 0 == symbol) return 2; else return 0; } int Score(char password[], int passwordLength) { int score = lenScore(password, passwordLength) + alphaScore(password, passwordLength) + digitScore(password, passwordLength) + symbolScore(password, passwordLength) + extraScore(password, passwordLength); return score; } int main() { char password[1000]; int passwordLength; int score = 0; while(scanf("%s", password) != EOF) { passwordLength = strlen(password); score = Score(password, passwordLength); //printf("%d\n", score); //printf("passwordLength:%d, len:%d, alpha:%d, digit:%d, symbol:%d, extra:%d\r\n", passwordLength, lenScore(password, passwordLength), alphaScore(password, passwordLength), digitScore(password, passwordLength), symbolScore(password, passwordLength), extraScore(password, passwordLength)); if(90 <= score) { printf("VERY_SECURE\n"); continue; } else if(80 <= score) { printf("SECURE\n"); continue; } else if(70 <= score) { printf("VERY_STRONG\n"); continue; } else if(60 <= score) { printf("STRONG\n"); continue; } else if(50 <= score) { printf("AVERAGE\n"); continue; } else if(20 <= score) { printf("WEAK\n"); continue; } else if(0 <= score) { printf("VERY_WEAK\n"); continue; } } return 0; }
C 解法, 执行用时: 1ms, 内存消耗: 336KB, 提交时间: 2021-09-08
#include<stdio.h> #include<string.h> int main() { int i=0,a=0,b=0,b1=0,b2=0,c=0,d=0,e=0; char ch; while(scanf("%c",&ch)!=EOF) { if(ch=='\n') { if(a>7) a=25; else if(a>4) a=10; else if(a<5) a=5; if(d==2) i=a+10*b+10*c+25+e; else i=a+10*b+10*c+10*d+e; if(i>=90)printf("VERY_SECURE\n"); else if(i>=80)printf("SECURE\n"); else if(i>=70)printf("VERY_STRONG\n"); else if(i>=60)printf("STRONG\n"); else if(i>=50)printf("AVERAGE\n"); else if(i>=25)printf("WEAK\n"); else if(i>=0)printf("VERY_WEAK\n"); i=a=b=b1=b2=c=d=e=0; } a++; if(b!=2) { if(b1==0) if(ch>='a'&&ch<='z') b1=1; if(b2==0) if(ch>='A'&&ch<='Z') b2=1; } b=b1+b2; if(c<2) if(ch>='0'&&ch<='9') c++; if(d<2) if(ch>='!'&&ch<='/'||ch>=':'&&ch<='@'||ch>='['&&ch<='`'||ch>='{'&&ch<='~') d++; if(b>0&&c>0) { if(d==0)e=2; if(d>0)e=3; if(b==2&&d>0)e=5; } } }