下面按照你的要求来讲:先给完整代码,再讲解题思路,再解释为什么要用到这些函数、语法、运算符,最后逐行解释代码。
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
int score = 0;
int lower = 0, upper = 0, digit = 0, symbol = 0;
// 1. 统计小写字母、大写字母、数字、符号的数量
for (char c : s) {
if (c >= 'a' && c <= 'z') lower++;
else if (c >= 'A' && c <= 'Z') upper++;
else if (c >= '0' && c <= '9') digit++;
else symbol++;
}
// 2. 密码长度得分
if (s.size() <= 4) score += 5;
else if (s.size() <= 7) score += 10;
else score += 25;
// 3. 字母得分
if (lower == 0 && upper == 0) score += 0;
else if (lower > 0 && upper > 0) score += 20;
else score += 10;
// 4. 数字得分
if (digit == 0) score += 0;
else if (digit == 1) score += 10;
else score += 20;
// 5. 符号得分
if (symbol == 0) score += 0;
else if (symbol == 1) score += 10;
else score += 25;
// 6. 奖励得分
if (lower > 0 && upper > 0 && digit > 0 && symbol > 0) score += 5;
else if ((lower > 0 || upper > 0) && digit > 0 && symbol > 0) score += 3;
else if ((lower > 0 || upper > 0) && digit > 0) score += 2;
// 7. 输出密码等级
if (score >= 90) cout << "VERY_SECURE" << endl;
else if (score >= 80) cout << "SECURE" << endl;
else if (score >= 70) cout << "VERY_STRONG" << endl;
else if (score >= 60) cout << "STRONG" << endl;
else if (score >= 50) cout << "AVERAGE" << endl;
else if (score >= 25) cout << "WEAK" << endl;
else cout << "VERY_WEAK" << endl;
return 0;
} 题目给你一个密码字符串,让你按照规则给密码打分。
一共分为几部分:
1. 密码长度得分 2. 字母得分 3. 数字得分 4. 符号得分 5. 奖励得分 6. 根据总分输出等级
例如密码:
38$@NoNoN
它的情况是:
长度大于等于 8,得 25 分 有大写字母 N,也有小写字母 o,得 20 分 有数字 3 和 8,数字超过 1 个,得 20 分 有符号 $ 和 @,符号超过 1 个,得 25 分 有大小写字母、数字、符号,奖励 5 分
总分:
25 + 20 + 20 + 25 + 5 = 95
所以输出:
VERY_SECURE
这道题属于 字符串模拟题。
所谓模拟题,就是题目怎么说,我们就怎么一步一步写代码。
这道题不需要复杂算法,只需要统计密码里面有多少种字符。
我们先遍历整个字符串,统计四类字符的数量:
int lower = 0; // 小写字母数量 int upper = 0; // 大写字母数量 int digit = 0; // 数字数量 int symbol = 0; // 符号数量
然后分别计算:
长度分 字母分 数字分 符号分 奖励分
最后根据总分输出对应等级。
代码中用了:
string s;
string是 C++ 中用来保存字符串的类型。
因为密码不是单个字符,而是一串字符,例如:
38$@NoNoN
这里面有数字、字母、符号,所以需要用字符串来保存。
如果用char,只能保存一个字符,例如:
char c = 'A';
但是密码是多个字符,所以不能用char保存整个密码,而要用:
string s;
代码中有:
#include <string>
这是为了使用 C++ 的string类型。
如果不写这个头文件,有些编译环境可能不认识:
string
虽然有些环境中只写:
#include <iostream>
也能使用string,但是规范写法是加上:
#include <string>
这样更稳妥。
代码中有:
cin >> s;
cin是 C++ 的输入对象,用来从键盘或者标准输入中读取数据。
>>是输入运算符,作用是把输入的内容放进变量里。
例如输入:
38$@NoNoN
执行:
cin >> s;
之后,变量s里面保存的就是:
38$@NoNoN
这里为什么不用getline(cin, s)?
因为题目输入的是一个密码字符串,中间没有空格,所以用:
cin >> s;
就够了。
如果输入中有空格,才需要考虑使用:
getline(cin, s);
但这道题不需要。
代码中有:
s.size()
size()是string类型自带的函数,用来获取字符串长度。
比如:
string s = "abc123";
那么:
s.size()
结果就是:
6
因为"abc123"一共有 6 个字符。
在这道题中,密码长度会影响得分:
小于等于 4 个字符:5 分 5 到 7 个字符:10 分 大于等于 8 个字符:25 分
所以必须知道密码长度。
因此要用:
s.size()
来判断长度分。
代码中有:
for (char c : s) { 这是 C++ 中的范围 for 循环。
它的作用是:依次取出字符串s中的每一个字符。
例如:
string s = "A1$";
执行:
for (char c : s)
循环时,c会依次等于:
'A' '1' '$'
也就是说,它可以帮我们一个字符一个字符地检查密码。
这道题需要统计密码里有多少小写字母、大写字母、数字和符号,所以必须遍历密码中的每个字符。
所以用:
for (char c : s)
非常合适,代码也比普通下标写法更简单。
普通下标写法是:
for (int i = 0; i < s.size(); i++) {
char c = s[i];
} 而范围 for 写法更简洁:
for (char c : s) {
} 对刚学 C++ 的学生来说,可以理解为:
把字符串 s 里面的字符一个一个拿出来,放到变量 c 里面。
代码中有:
if (c >= 'a' && c <= 'z') lower++; else if (c >= 'A' && c <= 'Z') upper++; else if (c >= '0' && c <= '9') digit++; else symbol++;
在 C++ 中,字符本质上可以按照 ASCII 编码进行比较。
例如小写字母在 ASCII 表中是连续的:
'a' 'b' 'c' ... 'z'
所以:
c >= 'a' && c <= 'z'
就可以判断字符c是不是小写字母。
同理,大写字母也是连续的:
'A' 'B' 'C' ... 'Z'
所以:
c >= 'A' && c <= 'Z'
可以判断是不是大写字母。
数字字符也是连续的:
'0' '1' '2' ... '9'
所以:
c >= '0' && c <= '9'
可以判断是不是数字字符。
注意这里的'0'是字符,不是数字0。
'0'
表示字符 0。
0
表示整数 0。
这两个不是一回事。
代码中写的是:
else symbol++;
意思是:
如果当前字符既不是小写字母,也不是大写字母,也不是数字,那么它就被认为是符号。
因为题目中的密码字符主要由:
字母 数字 符号
组成。
所以前面已经排除了字母和数字,剩下的就是符号。
例如:
$ @ ) + :
这些都不是字母,也不是数字,所以会进入:
else symbol++;
让符号数量加一。
代码中写的是:
if (lower > 0 && upper > 0 && digit > 0 && symbol > 0) score += 5; else if ((lower > 0 || upper > 0) && digit > 0 && symbol > 0) score += 3; else if ((lower > 0 || upper > 0) && digit > 0) score += 2;
题目说奖励分:
2 分:字母和数字 3 分:字母、数字和符号 5 分:大小写字母、数字和符号
而且只能选一种最高奖励。
如果一个密码同时有:
小写字母 大写字母 数字 符号
那它满足 5 分,也满足 3 分,也满足 2 分。
但是题目要求只能选符合最多的那一种,所以应该只加 5 分。
因此必须从最高分开始判断:
先判断 5 分 再判断 3 分 最后判断 2 分
不能反过来写。
如果先判断 2 分,那么一个很强的密码可能只加了 2 分,结果就错了。
例如:
lower++;
它等价于:
lower = lower + 1;
意思是让变量加一。
如果当前发现一个小写字母,就让lower加一。
比如密码中有三个小写字母,那么lower最后就是3。
例如:
score += 25;
它等价于:
score = score + 25;
意思是给总分加 25 分。
这道题不断累加分数,所以用+=很方便。
例如:
score += 10;
表示:
在原来的 score 基础上,再加 10 分。
例如:
c >= 'a' && c <= 'z'
&&表示“并且”。
这句话意思是:
c 大于等于 'a' 并且 c 小于等于 'z'
两个条件都成立,整体才成立。
例如:
c = 'm'
'm'在'a'到'z'之间,所以是小写字母。
例如:
lower > 0 || upper > 0
||表示“或者”。
这句话意思是:
有小写字母 或者 有大写字母
只要其中一个成立,就说明密码中有字母。
比如:
abc123
有小写字母,没有大写字母,也算有字母。
比如:
ABC123
有大写字母,没有小写字母,也算有字母。
所以判断“有没有字母”时,要用:
lower > 0 || upper > 0
例如:
digit == 0
==是判断是否相等。
它和=不一样。
digit == 0
表示判断digit是否等于0。
digit = 0
表示把0赋值给digit。
刚学 C++ 时一定要注意:
判断相等用 == 赋值用 =
例如:
if (score >= 90) cout << "VERY_SECURE" << endl; else if (score >= 80) cout << "SECURE" << endl; else if (score >= 70) cout << "VERY_STRONG" << endl;
else if表示“否则如果”。
它适合处理多个等级判断。
程序会从上往下判断:
如果 score >= 90,输出 VERY_SECURE 否则如果 score >= 80,输出 SECURE 否则如果 score >= 70,输出 VERY_STRONG ...
一旦某个条件成立,后面的条件就不会再判断。
#include <iostream>
引入输入输出库。
因为代码中需要使用:
cin cout endl
这些都在<iostream>里面。
如果不写这一行,程序可能会不认识cin和cout。
#include <string>
引入字符串库。
因为代码中使用了:
string s;
string是字符串类型,所以需要包含<string>。
using namespace std;
使用标准命名空间。
如果没有这一行,代码需要写成:
std::cin std::cout std::string std::endl
写了这一行后,可以简写成:
cin cout string endl
这样对初学者更简单。
int main() { 这是主函数。
C++ 程序从main函数开始执行。
int表示这个函数最后返回一个整数。
string s;
定义一个字符串变量s。
这个变量用来保存输入的密码。
例如输入:
38$@NoNoN
那么s就保存这个字符串。
cin >> s;
读取输入的密码字符串,并存到变量s中。
cin表示输入。
>>表示把输入内容放进右边的变量。
int score = 0;
定义整数变量score,表示密码总分。
一开始还没有算任何分,所以初始值是0。
后面会不断用:
score += 分数;
来累加分数。
int lower = 0, upper = 0, digit = 0, symbol = 0;
这一行一次定义了四个整数变量。
分别表示:
lower:小写字母数量 upper:大写字母数量 digit:数字数量 symbol:符号数量
它们初始都是0,因为刚开始还没有统计任何字符。
for (char c : s) { 遍历字符串s中的每一个字符。
每次循环时,当前字符会被放进变量c中。
例如:
string s = "A1$";
循环时:
第一次 c = 'A' 第二次 c = '1' 第三次 c = '$'
if (c >= 'a' && c <= 'z') lower++;
判断当前字符c是否是小写字母。
如果c在'a'到'z'之间,就说明它是小写字母。
如果是小写字母,就执行:
lower++;
让小写字母数量加一。
例如:
c = 'n';
'n'在'a'到'z'之间,所以lower加一。
else if (c >= 'A' && c <= 'Z') upper++;
如果当前字符不是小写字母,再判断它是不是大写字母。
如果c在'A'到'Z'之间,就说明它是大写字母。
如果是大写字母,就执行:
upper++;
让大写字母数量加一。
else if (c >= '0' && c <= '9') digit++;
如果当前字符既不是小写字母,也不是大写字母,再判断它是不是数字字符。
如果c在'0'到'9'之间,就说明它是数字。
如果是数字,就执行:
digit++;
让数字数量加一。
注意这里判断的是字符数字,不是整数数字。
比如:
'8'
这是字符。
else symbol++;
如果当前字符不是小写字母、不是大写字母、也不是数字,那么它就是符号。
所以执行:
symbol++;
让符号数量加一。
例如:
$ @ ) +
都会被统计为符号。
if (s.size() <= 4) score += 5;
判断密码长度是否小于等于 4。
s.size()表示字符串s的长度。
如果长度小于等于 4,根据题目规则,长度分是 5 分。
所以执行:
score += 5;
else if (s.size() <= 7) score += 10;
如果长度不是小于等于 4,那么继续判断是否小于等于 7。
因为前面已经排除了:
长度 <= 4
所以这里的条件:
s.size() <= 7
实际上表示:
长度在 5 到 7 之间
根据题目规则,得 10 分。
else score += 25;
如果长度既不是小于等于 4,也不是小于等于 7,那就说明长度大于等于 8。
根据题目规则,得 25 分。
if (lower == 0 && upper == 0) score += 0;
判断有没有字母。
如果小写字母数量是 0,并且大写字母数量也是 0,说明没有任何字母。
根据题目规则,字母得分是 0 分。
所以:
score += 0;
这一句其实可以不写,因为加 0 不影响结果。
但是写出来更直观,方便初学者对应题目规则。
else if (lower > 0 && upper > 0) score += 20;
如果密码中既有小写字母,又有大写字母,说明符合“大小写混合”。
根据题目规则,字母得分是 20 分。
例如:
NoNoN
里面有大写N,也有小写o,所以得 20 分。
else score += 10;
如果不是没有字母,也不是大小写混合,那么只剩下一种情况:
只有小写字母 或者 只有大写字母
根据题目规则,字母得分是 10 分。
例如:
abcdef
只有小写字母,得 10 分。
ABCDEF
只有大写字母,也得 10 分。
if (digit == 0) score += 0;
判断数字数量是否为 0。
如果没有数字,根据题目规则,数字得分是 0 分。
else if (digit == 1) score += 10;
如果数字数量正好是 1 个,根据题目规则,得 10 分。
例如:
abc1
只有一个数字1。
else score += 20;
如果数字数量不是 0,也不是 1,那么就说明数字数量大于 1。
根据题目规则,得 20 分。
例如:
abc12
有两个数字,所以得 20 分。
if (symbol == 0) score += 0;
判断符号数量是否为 0。
如果没有符号,根据题目规则,符号得分是 0 分。
else if (symbol == 1) score += 10;
如果符号数量正好是 1 个,根据题目规则,符号得分是 10 分。
例如:
abc1@
只有一个符号@。
else score += 25;
如果符号数量不是 0,也不是 1,那么说明符号数量大于 1。
根据题目规则,符号得分是 25 分。
例如:
abc1@$
有两个符号,所以得 25 分。
if (lower > 0 && upper > 0 && digit > 0 && symbol > 0) score += 5;
判断是否满足最高奖励 5 分。
条件是:
有小写字母 有大写字母 有数字 有符号
对应代码:
lower > 0 upper > 0 digit > 0 symbol > 0
四个条件都满足,说明密码包含:
大小写字母、数字和符号
根据题目规则,奖励 5 分。
else if ((lower > 0 || upper > 0) && digit > 0 && symbol > 0) score += 3;
如果不满足 5 分,再判断是否满足 3 分。
3 分的条件是:
有字母 有数字 有符号
这里的“有字母”可以是小写字母,也可以是大写字母。
所以用:
lower > 0 || upper > 0
表示:
有小写字母或者有大写字母
然后还要满足:
digit > 0 symbol > 0
表示有数字和符号。
满足这些条件,就奖励 3 分。
else if ((lower > 0 || upper > 0) && digit > 0) score += 2;
如果不满足 5 分,也不满足 3 分,再判断是否满足 2 分。
2 分条件是:
有字母 有数字
所以代码写成:
(lower > 0 || upper > 0) && digit > 0
表示:
有小写字母或者大写字母,并且有数字
满足就奖励 2 分。
if (score >= 90) cout << "VERY_SECURE" << endl;
如果总分大于等于 90,输出:
VERY_SECURE
cout用来输出。
endl表示换行。
else if (score >= 80) cout << "SECURE" << endl;
如果分数没有达到 90,但是大于等于 80,输出:
SECURE
注意,因为前面已经判断过:
score >= 90
所以能走到这里,说明score一定小于 90。
因此这一行实际表示:
80 <= score < 90
else if (score >= 70) cout << "VERY_STRONG" << endl;
如果分数在 70 到 79 之间,输出:
VERY_STRONG
else if (score >= 60) cout << "STRONG" << endl;
如果分数在 60 到 69 之间,输出:
STRONG
else if (score >= 50) cout << "AVERAGE" << endl;
如果分数在 50 到 59 之间,输出:
AVERAGE
else if (score >= 25) cout << "WEAK" << endl;
如果分数在 25 到 49 之间,输出:
WEAK
else cout << "VERY_WEAK" << endl;
如果前面的条件都不满足,说明分数小于 25。
所以输出:
VERY_WEAK
return 0;
表示程序正常结束。
main函数返回0,一般表示程序运行成功。
输入:
38$@NoNoN
字符串内容是:
3 8 $ @ N o N o N
统计结果:
小写字母 lower = 2 // o, o 大写字母 upper = 3 // N, N, N 数字 digit = 2 // 3, 8 符号 symbol = 2 // $, @
长度是 9,所以:
长度得分:25
大小写字母都有,所以:
字母得分:20
数字数量大于 1,所以:
数字得分:20
符号数量大于 1,所以:
符号得分:25
同时有小写字母、大写字母、数字、符号,所以:
奖励得分:5
总分:
25 + 20 + 20 + 25 + 5 = 95
所以输出:
VERY_SECURE
因为它没有使用复杂函数,也没有使用数组、哈希表、位运算等高级写法。
只用了初学者常见内容:
1. string 字符串 2. cin 输入 3. cout 输出 4. for 循环 5. if else 判断 6. 字符比较 7. 变量计数 8. 分数累加
这道题的核心不是算法复杂,而是要把题目的规则完整翻译成代码。
只要先统计出小写字母、大写字母、数字、符号的数量,后面的打分就很容易写出来。
from functools import reduce
while True:
try:
s = input()
password = []
password.extend(s)
p1,p2,p3,p4 = [],[],[],[]
for i in password:
if i.isupper():
p1.append(i)
elif i.islower():
p2.append(i)
elif i.isdigit():
p3.append(i)
else:
p4.append(i)
score = []
if len(s) <= 4:
score.append(5)
elif 5 <= len(s) <= 7:
score.append(10)
else:
score.append(25)
if p1 or p2:
if len(p1) == 0 or len(p2) == 0:
score.append(10)
else:
score.append(20)
if len(p3) == 1:
score.append(10)
elif len(p3) > 1:
score.append(20)
if len(p4) == 1:
score.append(10)
elif len(p4) > 1:
score.append(25)
if (p1 or p2) and p3:
score.append(2)
elif (p1 or p2) and p3 and p4:
score.append(3)
elif p1 and p2 and p3 and p4:
score.append(5)
result = reduce(lambda x,y : x+y,score)
if result >= 90:
print('VERY_SECURE')
elif result >= 80:
print('SECURE')
elif result >= 70:
print('VERY_STRONG')
elif result >= 60:
print('STRONG')
elif result >= 50:
print('AVERAGE')
elif result >= 25:
print('WEAK')
elif result >= 0:
print('VERY_WEAK')
except:break #include<iostream>
#include<string>
#include<vector>
using namespace std;
int get(string str)
{
int sum=0,a1=0,a2=0,a3=0,a4=0,a5=0;
vector<char>a21,a22,a31,a41;
int len=str.size();
if(len<=4)a1=5;
else if(len>=8)a1=25;
else a1=10;
for(int i=0;i<len;++i)
if(str[i]<='z'&&str[i]>='a')a21.push_back(str[i]);
else if(str[i]<='Z'&&str[i]>='A')a22.push_back(str[i]);
else if(str[i]<='9'&&str[i]>='0')a31.push_back(str[i]);
else a41.push_back(str[i]);
int lena21=a21.size(),lena22=a22.size(),lena31=a31.size(),lena41=a41.size();
if(lena21==0&&lena22==0)a2=0;
else if(lena21!=0&&lena22!=0)a2=20;
else a2=10;
if(lena31==0)a3=0;
else if(lena31==1)a3=10;
else a3=20;
if(lena41==0)a4=0;
else if(lena41==1)a4=10;
else a4=25;
if(lena21&&lena22&&lena31&&lena41)a5=5;
else if((lena21||lena22)&&lena31&&lena41)a5=3;
else if((lena21||lena22)&&lena31)a5=2;
else a5=0;
sum=a1+a2+a3+a4+a5;
return sum;
}
int main()
{
string str;
while(cin>>str)
{
int sum=get(str);
switch(sum/10)
{
case 9:cout<<"VERY_SECURE"<<endl;break;
case 8:cout<<"SECURE"<<endl;break;
case 7:cout<<"VERY_STRONG"<<endl;break;
case 6:cout<<"STRONG"<<endl;break;
case 5:cout<<"AVERAGE"<<endl;break;
default:
{
if(sum>=25)cout<<"WEAK"<<endl;
else cout<<"VERY_WEAK"<<endl;
}
}
}
return 0;
}
import java.util.*;
import java.io.*;
public class Main {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String input = scanner.nextLine();
GetPwdSecurityLevel(input);
}
}
public static void GetPwdSecurityLevel(String input) {
char[] inputArray = input.toCharArray();
int score = 0;
int letterNum = 0;
int lowercaseNum = 0;
int uppercaseNum = 0;
int digitNum = 0;
int charNum = 0;
// count all type char nums
for (char c : inputArray) {
// if is lowercase letter
if (Character.isLowerCase(c)) {
lowercaseNum++;
letterNum++;
}
// if is uppercase letter
else if (Character.isUpperCase(c)) {
uppercaseNum++;
letterNum++;
}
// if is digits
else if (Character.isDigit(c)) {
digitNum++;
}
// if is character
else {
charNum++;
}
}
// password length
// length <= 4
int passLength = inputArray.length;
if (passLength <= 4) {
score += 5;
}
// 5 <= length <= 7
else if (passLength >= 5 && passLength <= 7) {
score += 10;
}
// length >= 8;
else {
score += 25;
}
// letter
// no letters
if (letterNum < 1) {
score += 0;
}
else if (letterNum > 0) {
// only lowercase or only uppercase
if (lowercaseNum < 1 || uppercaseNum < 1) {
score += 10;
}
// contain both lowercase and uppercase
else {
score += 20;
}
}
// digits
// no digits
if (digitNum < 1) {
score += 0;
}
// only one digit
else if (digitNum == 1) {
score += 10;
}
// more than one digits
else {
score += 20;
}
// character
// no character
if (charNum < 0) {
score += 0;
}
// only one character
else if (charNum == 1) {
score += 10;
}
// char.num > 1
else {
score += 25;
}
// award
// contain both letter and digit
if (letterNum > 0 && digitNum > 0) {
score += 2;
}
// contain both letter, digit, and character
if (letterNum > 0 && digitNum > 0 && charNum > 0) {
score += 3;
}
// contain both lowercase, uppercase, digit, and character
if (lowercaseNum > 0 && uppercaseNum > 0 && digitNum > 0 && charNum > 0) {
score += 5;
}
String secLevel = new String();
// judge security level
if (score >= 90) {
secLevel = "VERY_SECURE";
}
else if (score >= 80) {
secLevel = "SECURE";
}
else if (score >= 70) {
secLevel = "VERY_STRONG";
}
else if (score >= 60) {
secLevel = "STRONG";
}
else if (score >= 50) {
secLevel = "AVERAGE";
}
else if (score >= 25) {
secLevel = "WEAK";
}
else if (score >= 0) {
secLevel = "VERY_WEAK";
}
System.out.println(secLevel);
}
} // 1.将输入的字符串转换成字符数组,可以顺便取得密码长度
// 2.定义一个整型变量score用于记录最终的得分
// 3.定义三个整型变量letterNum、upperCaseNum、lowerCaseNum,分别记录所有字母数量、大写字母数量、小写字母数量
// 4.定义一个整型变量digitNum,用于记录数字字符的数量
// 5.定义一个charNum,用于记录符号的数量
// 6.遍历字符数组,统计每种字符的数量,然后确定总分
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String s = in.nextLine();//考虑密码中可能有空格,所以要接收空格
char[] cs = s.toCharArray();
int score = 0;// 最终的得分
int letterNum = 0;// 所有字母的数量
int upperCaseNum = 0;// 大写字母的数量
int lowerCaseNum = 0;// 小写字母的数量
int digitNum = 0;// 数字字符的数量
int charNum = 0;// 符号的数量
// 遍历字符数组,统计每种字符的数量
for(char c : cs){
if(Character.isLowerCase(c)){
lowerCaseNum++;
letterNum++;
}else if(Character.isUpperCase(c)){
upperCaseNum++;
letterNum++;
}else if(Character.isDigit(c)){
digitNum++;
}else{
charNum++;
}
}
// 根据密码长度来确定得分
if(cs.length <= 4){
score += 5;
}else if(cs.length <= 7){
score += 10;
}else if(cs.length >= 8){
score += 25;
}
// 根据字母情况来确定得分
if(letterNum < 1){
score += 0;
}else if(letterNum == upperCaseNum || letterNum == lowerCaseNum){
score += 10;
}else if(upperCaseNum > 0 && lowerCaseNum > 0){
score += 20;
}
// 根据数字的情况来确定得分
if(digitNum < 1){
score += 0;
}else if(digitNum == 1){
score += 10;
}else if(digitNum > 1){
score += 20;
}
// 根据符号的情况来确定得分
if(charNum < 1){
score += 0;
}else if(charNum == 1){
score += 10;
}else if(charNum > 1){
score += 25;
}
// 根据奖励的情况来确定得分,注意这个逻辑是三选一,所以要倒着来
if(upperCaseNum > 0 && lowerCaseNum > 0 && digitNum > 0 && charNum > 0){
score += 5;
}else if((upperCaseNum > 0 && digitNum > 0 && charNum > 0) || (lowerCaseNum > 0 && digitNum > 0 && charNum > 0)){
score += 3;
}else if((upperCaseNum > 0 && digitNum > 0) || (lowerCaseNum > 0 && digitNum > 0)){
score += 2;
}
// 输出结果
if(score >= 90){
System.out.println("VERY_SECURE");
}else if(score >= 80){
System.out.println("SECURE");
}else if(score >= 70){
System.out.println("VERY_STRONG");
}else if(score >= 60){
System.out.println("STRONG");
}else if(score >= 50){
System.out.println("AVERAGE");
}else if(score >= 25){
System.out.println("WEAK");
}else if(score >= 0){
System.out.println("VERY_WEAK");
}
}
in.close();
}
} def longenth_score(pwd):
if len(pwd)<=4:
return 5
elif len(pwd)>=8:
return 25
else:
return 10
def char_score(pwd):
pn =0
for p in pwd:
if p.isalpha():
pn+=1
if pn==0:
return 0
elif pwd.upper()==pwd&nbs***bsp;pwd.lower()==pwd:
return 10
else:
return 20
def num_score(pwd):
nn = 0
for n in pwd:
if n.isdigit():
nn +=1
if nn ==0:
return 0
elif nn ==1:
return 10
else:
return 20
def sym_score(pwd):
for i in pwd:
if i.isdigit()&nbs***bsp;i.isalpha():
pwd = pwd.replace(i,'0')
sn = len(pwd) - pwd.count('0')
if sn == 0:
return 0
elif sn ==1 :
return 10
else:
return 25
def reward_score(c_score,n_score,s_score):
if c_score == 20 and n_score>0 and s_score>0:
return 5
elif c_score == 10 and n_score>0 and s_score>0:
return 3
elif c_score > 10 and n_score>0 and s_score==0:
return 2
else:
return 0
def pwd_level(total_score):
if total_score>=90:
return 'VERY_WEAK'
elif total_score >=80:
return 'WEAK'
elif total_score >=70:
return 'VERY_STRONG'
elif total_score >=60:
return 'STRONG'
elif total_score >=50:
return 'AVERAGE'
elif total_score >=25:
return 'SECURE'
else:
return 'VERY_SECURE'
if __name__ == '__main__':
while True:
try:
pwd = input()
l_score = longenth_score(pwd)
c_score = char_score(pwd)
n_score = num_score(pwd)
s_score = sym_score(pwd)
r_score = reward_score(c_score,n_score,s_score)
total_score = l_score+c_score+n_score+s_score+r_score
print(pwd_level(total_score))
except:
break 笨方法,还算清晰,示例是错的差点坑死我
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s = sc.nextLine();
int[] arr = new int[5];
// 1.长度
if (s.length() <= 4) {
arr[0] = 5;
} else if(5<=s.length() && s.length()<=7) {
arr[0] = 10;
} else{
arr[0] = 25;
}
// 2.字母
int lowerCount = 0;
int upperCount = 0;
int digitCount = 0;
int symbolCount = 0;
for(int i=0; i<s.length();i++) {
if (Character.isUpperCase(s.charAt(i))) {
upperCount++;
} else if (Character.isLowerCase(s.charAt(i))) {
lowerCount++;
}else if (Character.isDigit(s.charAt(i))) {
digitCount++;
} else{ // 这里包括了字符串中可能存在的空格
symbolCount++;
}
}
if(lowerCount == 0 && upperCount == 0) {
arr[1] = 0;
} else if (lowerCount !=0 && upperCount != 0) {
arr[1] = 20;
} else if (lowerCount != 0 || upperCount != 0) {
arr[1] = 10;
}
// 3.数字
if (digitCount == 0) {
arr[2] = 0;
} else if (digitCount == 1) {
arr[2] = 10;
} else if (digitCount > 1) {
arr[2] = 20;
}
// 4.符号
if (symbolCount == 0) {
arr[3] = 0;
} else if (symbolCount == 1) {
arr[3] = 10;
} else if (symbolCount > 1) {
arr[3] = 25;
}
// 5.奖励
if ((lowerCount+upperCount) > 0 && digitCount > 0 && symbolCount == 0) {
arr[4] = 2;
}else if (lowerCount>0 && upperCount > 0 && digitCount > 0 && symbolCount > 0) {
arr[4] = 5;
} else if ((lowerCount+upperCount) > 0 && digitCount > 0 && symbolCount > 0) {
arr[4] = 3;
}
int totalCount = arr[0] + arr[1] + arr[2] + arr[3] + arr[4];
if (totalCount >= 90) System.out.println("VERY_SECURE");
if (totalCount >= 80 && totalCount < 90) System.out.println("SECURE");
if (totalCount >= 70 && totalCount < 80) System.out.println("VERY_STRONG");
if (totalCount >= 60 && totalCount < 70) System.out.println("STRONG");
if (totalCount >= 50 && totalCount < 60) System.out.println("AVERAGE");
if (totalCount >= 25 && totalCount < 50) System.out.println("WEAK");
if (totalCount >= 0 && totalCount < 25) System.out.println("VERY_WEAK");
}
}
} #include <bits/stdc++.h>
using namespace std;
int length(string s)
{
int res;
int n=s.size();
if(n<=4) res=5;
else if(n<=7) res=10;
else res=25;
return res;
}
int alp(string s)
{
int res,res1=0,res2=0;
int n=s.size();
for(int i=0;i<n;i++)
{
if(s[i]>='a' && s[i]<='z') res1++;
else if(s[i]>='A' && s[i]<='Z') res2++;
}
if(res1==0 && res2==0) res=0;
else if(res1==0 || res2==0) res=10;
else res=20;
return res;
}
int dig(string s)
{
int res,res1=0;
int n=s.size();
for(int i=0;i<n;i++)
{
if(s[i]>='0' && s[i]<='9') res1++;
}
if(res1==0) res=0;
else if(res1==1) res=10;
else res=20;
return res;
}
int sym(string s)
{
int res,res1=0;
int n=s.size();
for(int i=0;i<n;i++)
{
if((s[i]>=33 && s[i]<=47) || (s[i]>=58 && s[i]<=64) || (s[i]>=91 && s[i]<=96) || (s[i]>=123 && s[i]<=126) )
res1++;
}
if(res1==0) res=0;
else if(res1==1) res=10;
else res=25;
return res;
}
int main()
{
string s;
while(getline(cin,s))
{
int res = length(s)+alp(s)+dig(s)+sym(s);
if((alp(s)>10) && dig(s) && sym(s)) res+=5;
else if(alp(s) && dig(s) && sym(s)) res += 3;
else if(dig(s) && alp(s) ) res+=2;
if(res>=90) cout<<"VERY_SECURE"<<endl;
else if(res>=80) cout<<"SECURE"<<endl;
else if(res>=70) cout<<"VERY_STRONG"<<endl;
else if(res>=60) cout<<"STRONG"<<endl;
else if(res>=50) cout<<"AVERAGE"<<endl;
else if(res>=25) cout<<"WEAK"<<endl;
else cout<<"VERY_WEAK"<<endl;
}
system("pause");
return 0;
}
import java.util.Scanner;
public class Main {
public static String GetPwdSecurityLevel(String Password) {
int score = 0;
int length = Password.length();
if (length <= 4)
score += 5;
else if (length <= 7)
score += 10;
else
score += 25;
char[] c = Password.toCharArray();
int[] type = new int[4];
int countNum = 0;
int countSign = 0;
for (int i = 0; i < length; ++i) {
if (c[i] >= 'a' && c[i] <= 'z')
type[0] = 1;
else if (c[i] <= 'Z' && c[i] >= 'A')
type[1] = 1;
else if ((c[i] >= '0' && c[i] <= '9')) {
type[2] = 1;
++countNum;
} else {
type[3] = 1;
++countSign;
}
}
if ((type[0] & type[1]) == 1) {
score += 20;
if ((type[3] & type[2]) == 1)
score += 5;
else if (type[2] == 1)
score += 2;
} else if ((type[0] | type[1]) == 1) {
score += 10;
if ((type[3] & type[2]) == 1)
score += 3;
else if (type[2] == 1)
score += 2;
}
if (countNum == 1) {
score += 10;
} else if (countNum > 1) {
score += 25;
}
if (countSign == 1) {
score +=10;
} else if (countSign > 1) {
score += 25;
}
if (score >= 90)
return "VERY_SECURE";
else if (score >= 80)
return "SECURE";
else if (score >= 70)
return "VERY_STRONG";
else if (score >= 60)
return "STRONG";
else if (score >= 50)
return "AVERAGE";
else if (score >= 25)
return "WEAK";
else if (score >= 0)
return "VERY_WEAK";
return "Wrong";
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String pass = "";
while (scanner.hasNext()) {
pass = scanner.nextLine();
System.out.println(GetPwdSecurityLevel(pass));
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String line = sc.nextLine();
int score = 0;
if(line.length()>=8) {
score += 25;
} else if(line.length()>=5) {
score += 10;
} else if(line.length()<=4) {
score += 5;
}
int upplet = 0;
int dowlet = 0;
int numlet = 0;
int othlet = 0;
for(int i=0; i< line.length(); i++) {
if(line.charAt(i)>='A' && line.charAt(i)<='Z') {
upplet ++;
} else if(line.charAt(i)>='a' && line.charAt(i)<='z') {
dowlet ++;
} else if(line.charAt(i)>='0' && line.charAt(i)<='9') {
numlet ++;
} else {
othlet ++;
}
}
if((upplet!=0 && dowlet==0) || (upplet==0 && dowlet!=0)) {
score += 10;
} else if(upplet!=0 && dowlet!=0) {
score += 20;
}
if(numlet == 1) {
score += 10;
} else if(numlet > 1) {
score += 20;
}
if(othlet == 1) {
score += 10;
} else if(othlet > 1) {
score += 25;
}
if(numlet!=0 && upplet!=0 && dowlet!=0 && othlet!=0) {
score += 5;
} else if(numlet!=0 && (upplet!=0 || dowlet!=0) && othlet!=0) {
score += 3;
} else if(numlet!=0 && (upplet!=0 || dowlet!=0)) {
score += 2;
}
if(score>=90)
System.out.println("VERY_SECURE");
else if(score>=80)
System.out.println("SECURE");
else if(score>=70)
System.out.println("VERY_STRONG");
else if(score>=60)
System.out.println("STRONG");
else if(score>=50)
System.out.println("AVERAGE");
else if(score>=25)
System.out.println("WEAK");
else if(score>=0)
System.out.println("VERY_WEAK");
}
}
} import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String str = "";
while ((str = bufferedReader.readLine()) != null) {
int number = 0;
int len = str.length();
int character1 = 0, character2 = 0;
int numberal = 0;
int symbol = 0;
int reward = 0;
char[] chars = str.toCharArray();
while (number < len) {
if ((chars[number] >= 'a' && chars[number] <= 'z')) {
character1++;
} else if(chars[number] >= 'A' && chars[number] <= 'Z') {
character2++;
} else if (chars[number] >= '0' && chars[number] <= '9') {
numberal++;
} else {
symbol++;
}
number++;
}
int result = 0;
if (len < 5) {
result += 5;
} else if (len < 8) {
result += 10;
} else {
result += 25;
}
if ((character1 > 0 && character2 == 0) || (character2 > 0 && character1 == 0)) {
result += 10;
} else if (character1 > 0 && character2 > 0) {
result += 20;
}
if (numberal == 1) {
result += 10;
} else if (numberal > 1) {
result += 20;
}
if (symbol == 1) {
result += 10;
} else if (symbol > 1) {
result += 25;
}
if (character1 > 0 && character2 > 0 && numberal > 0 && symbol > 0) {
result += 5;
} else if ((character1 > 0 || character2 > 0) && numberal > 0 && symbol > 0) {
result += 3;
} else if ((character1 > 0 || character2 > 0) && numberal > 0) {
result += 2;
}
switch(result / 10) {
case 0:
case 1:
case 2:
if (result < 25) {
System.out.println("VERY_WEAK");
} else {
System.out.println("WEAK");
}
break;
case 3:
case 4:
System.out.println("WEAK");
break;
case 5:
System.out.println("AVERAGE");
break;
case 6:
System.out.println("STRONG");
break;
case 7:
System.out.println("VERY_STRONG");
break;
case 8:
System.out.println("SECURE");
break;
case 9:
case 10:
case 11:
System.out.println("VERY_SECURE");
break;
default:
break;
}
}
}
} import java.util.*;
/**
* @author hll[yellowdradra@foxmail.com]
* @description 密码强度等级
* @date 2021-05-13 23:34
**/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String password = scanner.nextLine();
int numberCount = 0;
int lowerLetterCount = 0;
int upperLetterCount = 0;
int symbolCount = 0;
int difficultyGoal = 0;
int len = password.length();
for (int i = 0; i < password.length(); i++) {
char c = password.charAt(i);
if (c <= '9' && c >= '0') {
numberCount++;
} else if (c <= 'z' && c >= 'a') {
lowerLetterCount++;
} else if (c <= 'Z' && c >= 'A') {
upperLetterCount++;
} else {
symbolCount++;
}
}
difficultyGoal += len <= 4 ? 5 : len < 8 ? 10 : 25;
difficultyGoal += lowerLetterCount > 0 && upperLetterCount > 0 ? 20
: lowerLetterCount != upperLetterCount && (lowerLetterCount == 0 || upperLetterCount == 0)? 10 : 0;
difficultyGoal += numberCount == 0 ? 0 : numberCount == 1 ? 10 : 20;
difficultyGoal += symbolCount == 0 ? 0 : symbolCount == 1 ? 10 : 25;
difficultyGoal += lowerLetterCount > 0 && upperLetterCount > 0 && numberCount > 0 && symbolCount > 0 ? 5
: lowerLetterCount == 0 || upperLetterCount == 0 ? 3 : symbolCount == 0 ? 2 : 0;
String[] levels = {"AVERAGE", "STRONG", "VERY_STRONG", "SECURE", "VERY_SECURE"};
String difficultyLevel = difficultyGoal < 25 ? "VERY_WEAK"
: difficultyGoal < 50 ? "WEAK"
: levels[(difficultyGoal / 10) - 5];
System.out.println(difficultyLevel);
}
}
}
# 思路:将统计情况放主函数中一起写,避免分开写时多次遍历密码字符串。
while True:
try:
password = input().strip()
score = 0 # 初始化得分为0
# 长度得分
if len(password) <= 4:
score += 5
elif 5 <= len(password) <= 7:
score += 10
else:
score += 25
# 字母、数字、符号统计(是否出现过、出现的个数)
alpha, Alpha, digit, digit_num, symbol, symbol_num = 0, 0, 0, 0, 0, 0
for ch in password:
if ch.islower():
alpha = 1
elif ch.isupper():
Alpha = 1
elif ch.isdigit():
digit = 1
digit_num += 1
else:
symbol = 1
symbol_num += 1
# 字母得分
if (alpha and not Alpha) or (Alpha and not alpha):
score += 10
elif alpha and Alpha:
score += 20
# 数字得分
if digit_num == 1:
score += 10
elif digit_num > 1:
score += 20
# 符号得分
if symbol_num == 1:
score += 10
elif symbol_num > 1:
score += 25
# 奖励得分
# 此写法错误,本该属于第三种加分的情况被第二种加分情况拦截住了,加成了第二种情况的分数。
# if (alpha or Alpha) and digit and not symbol:
# score += 2
# elif (alpha or Alpha) and digit and symbol:
# score += 3
# elif alpha and Alpha and digit and symbol:
# score += 5
if alpha and Alpha and digit and symbol:
score += 5
elif (alpha or Alpha) and digit and symbol:
score += 3
elif (alpha or Alpha) and digit:
score += 2
# 分数等级
if score >= 90:
print('VERY_SECURE')
elif score >= 80:
print('SECURE')
elif score >= 70:
print('VERY_STRONG')
elif score >= 60:
print('STRONG')
elif score >= 50:
print('AVERAGE')
elif score >= 25:
print('WEAK')
else:
print('VERY_WEAK')
except:
break import string
import sys
def check_passwd(pwd):
score = 0
pwd_sign = [False for _ in range(4)]
pwd_types = [string.ascii_uppercase, string.ascii_lowercase, string.digits, string.punctuation]
pwd_dict = dict.fromkeys(pwd_types, 0)
for s in pwd:
for key in pwd_dict.keys():
if s in key:
pwd_dict[key] += 1
if pwd_dict[string.ascii_uppercase] > 0:
score += 10
pwd_sign[0] = True
if pwd_dict[string.ascii_uppercase] > 0:
score += 10
pwd_sign[1] = True
if pwd_dict[string.digits] >= 1:
score += 10
pwd_sign[2] = True
if pwd_dict[string.digits] > 1:
score += 10
if pwd_dict[string.punctuation] >= 1:
score += 10
pwd_sign[3] = True
if pwd_dict[string.punctuation] > 1:
score += 15
if pwd_sign[0]&nbs***bsp;pwd_sign[1]:
if pwd_sign[1]:
score += 2
if pwd_sign[2]:
score += 1
if all(pwd_sign):
score += 1
# length
pwd_length = len(pwd)
if pwd_length < 5:
score += 5
elif pwd_length <= 7:
score += 10
else:
score += 25
if score >= 90:
print('VERY_SECURE')
elif 80 <= score < 90:
print('SECURE')
elif 70 <= score < 80:
print('VERY_STRONG')
elif 60 <= score < 70:
print('STRONG')
elif 50 <= score < 60:
print('AVERAGE')
elif 25 <= score < 50:
print('WEAK')
else:
print('VERY_WEAK')
if __name__ == '__main__':
for line in sys.stdin:
check_passwd(line)
import java.util.Scanner;
public class Main {
public static void getLevel(int score){
if (score>=90){
System.out.println("VERY_SECURE");
}else if (score>=80){
System.out.println("SECURE");
}else if (score>=70){
System.out.println("VERY_STRONG");
}else if (score>=60){
System.out.println("STRONG");
}else if (score>=50){
System.out.println("AVERAGE");
}else if (score>=25){
System.out.println("WEAK");
}else if (score>=0){
System.out.println("VERY_WEAK");
}
}
public static int judgeRule(String str){
int score = 0;
char[] chars = str.toCharArray();
//密码长度计分
if (str.length()>=8){
score += 25;
}else if (str.length()>=5){
score +=10;
}else {
score +=5;
}
//字母、数字、符号出现情况计分
int upperCount = 0;
int lowerCount = 0;
int numberCount = 0;
int symbolCount = 0;
for (int i = 0; i < chars.length; i++) {
if (Character.isLetter(chars[i])){
if (Character.isUpperCase(chars[i])){
upperCount++;
}else {
lowerCount++;
}
}else if (Character.isDigit(chars[i])){
numberCount++;
}else {
symbolCount++;
}
}
if (upperCount != 0 && lowerCount !=0){
score +=20;
}
if (upperCount == str.length() || lowerCount == str.length()){
score +=10;
}
if (numberCount>1){
score +=20;
}else if (numberCount ==1){
score +=10;
}
if (symbolCount>1){
score +=25;
}else if (symbolCount ==1){
score +=10;
}
//奖励计分
if (upperCount!=0 && lowerCount !=0 && numberCount !=0 && symbolCount != 0){
score +=5;
}
if ((upperCount == str.length() || lowerCount == str.length()) && numberCount !=0){
score +=3;
}
if ((upperCount + lowerCount !=0 && numberCount !=0)){
score +=2;
}
return score;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.next();
getLevel(judgeRule(str));
}
}
} //题目是很简单的,就是要注意细节,真的很容易错,条件太多了,要仔细
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char ch[500];
while(cin>>ch){
int len=strlen(ch),score=0,da=0,xiao=0,num=0,sign=0;
if(len<=4) score=5;
else if(len>=5&&len<=7) score=10;
else score=25;
for(int i=0;i<len;i++){
if(ch[i]>='a'&&ch[i]<='z')
xiao++;
else if(ch[i]>='A'&&ch[i]<='Z')
da++;
else if(ch[i]>='0'&&ch[i]<='9')
num++;
else sign++;
}
if(da>0) score+=10;
if(xiao>0) score+=10;
if(num==1) score+=10;
else if(num>1) score+=20;
if(sign==1) score+=10;
else if(sign>1) score+=25;
if((da>0&&num>0)||(xiao>0&&num>0)){
score+=2;
if(sign>0){
score+=1;
if(da>0&&num>0&&xiao>0)
score+=2;
}
}
if(0<=score&&score<25)
cout<<"VERY_WEAK"<<endl;
else if(25<=score&&score<50)
cout<<"WEAK"<<endl;
else if(50<=score&&score<60)
cout<<"AVERAGE"<<endl;
else if(60<=score&&score<70)
cout<<"STRONG"<<endl;
else if(70<=score&&score<80)
cout<<"VERY_STRONG"<<endl;
else if(80<=score&&score<90)
cout<<"SECURE"<<endl;
else if(90<=score)
cout<<"VERY_SECURE"<<endl;
}
}