题解 | #密码强度等级#
密码强度等级
http://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
思路
- 按照题目一步一步分析编写代码
- 统计输出结果
Answer
#include<stdio.h>
#include<string.h>
int main() {
char str[300];
int score = 0;
scanf("%s",str);
int len = strlen(str);
//长度
if (len <= 4) {
score += 5;
} else if (len <= 7) {
score += 10;
} else if (len >= 8) {
score += 25;
}
//字母
int flg_a = 0, flg_A = 0;
int num_cnt = 0;
int symbol = 0;
for (int i = 0; i < len; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
flg_a = 1;
} else if (str[i] >= 'A' && str[i] <= 'Z') {
flg_A = 1;
} else if (str[i] >= '0' && str[i] <= '9') {
num_cnt++;
} else {
symbol++;
}
}
if ((flg_A == 0) && (flg_a == 0)) {
score += 0;
} else if ((flg_A == 1) && (flg_a == 1)) {
score += 20;
} else {
score += 10;
}
//数字
if (num_cnt <= 0) {
score += 0;
} else if (num_cnt == 1) {
score += 10;
} else if (num_cnt > 1) {
score += 20;
}
//符号
if (symbol <= 0) {
score += 0;
} else if (symbol == 1) {
score += 10;
} else if (symbol > 1) {
score += 25;
}
//奖励
int result = 0;
if (((flg_A == 1) || (flg_a == 1)) && (num_cnt >= 1)) {
result = 2;
}
if (((flg_A == 1) || (flg_a == 1)) && (num_cnt >= 1) && (symbol >= 1)) {
result = 3;
}
if ((flg_A == 1) && (flg_a == 1) && (num_cnt >= 1) && (symbol >= 1)) {
result = 5;
}
if ((score + result) >= 90) {
printf("VERY_SECURE\n");
}else if ((score + result) >= 80) {
printf("SECURE\n");
}else if ((score + result) >= 70) {
printf("VERY_STRONG\n");
}else if ((score + result) >= 60) {
printf("STRONG\n");
}else if ((score + result) >= 50) {
printf("AVERAGE\n");
}else if ((score + result) >= 25) {
printf("WEAK\n");
}else if ((score + result) >= 0) {
printf("VERY_WEAK\n");
}
return 0;
}