第二题、密码强度
描述
网站提示,密码必须由8~16个字符组成,可以包含数字、大写字母、小写字母、特殊符号这4种字符类型。
注:特殊符号只包含:!、@、#、$、%、^、&、*、(、)、_、+、-、=。
以下是三种强度密码的设计规则:
1)包含4种不同类型字符的密码是强密码,
2)包含2种或3种不同类型字符的密码是中等密码;
3)只包含1种类型字符的密码是弱密码。
小威利用浏览器自动创建了N个密码,请你编写程序判断这些密码的强度
输入
第一行输入一个正整数N(4≤N≤10),表示密码的个数。
接下来的N行,每行输入一个字符串Si(8≤Si的长度≤16),表示一个密码,密码中可能包含数字、大写字母、小写字母、特殊符号这4种字符类型,且不含空格字符
输出
共N行,每行输出一个整数Ri(Ri只能是0、1、2),依次表示对应密码Si的强度
如果Si是强密码,则输出2
如果Si是中等密码,则输出1:
如果Si是弱密码,则输出0
样例输入:
12345678
ASDF1234
ABcde67890A1b2c3d$e%
样例输出:
0
1
1
2
C++实现
#include<iostream>
#include <cctype>
using namespace std;
string specialChar = "!@#$%^&*()_+-=";
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
string pwd;
cin>>pwd;
if(pwd.size()<8 || pwd.size()>16){
cout<<0;
}else{
bool has_digit =false;
bool has_upper = false;
bool has_lower = false;
bool has_special = false;
for(char c:pwd){
if(isdigit(c)){
has_digit =true;
}else if(islower(c)){
has_lower = true;
}else if(isupper(c)){
has_upper = true;
}else if(specialChar.find(c) != string::npos){
has_special=true;
}
}
int ans=0;
if(has_digit) ans++;
if(has_lower) ans++;
if(has_upper) ans++;
if(has_special) ans++;
if(ans==1){
cout<<0<<endl;
}else if(ans==4){
cout<<2<<endl;
}else{
cout<<1<<endl;
}
}
}
return 0;
}

后续
接下来我会不断用C++来实现信奥比赛中的算法题、GESP考级编程题实现、白名单赛事考题实现,记录日常的编程生活、比赛心得,感兴趣的请关注,我后续将继续分享相关内容

1678

被折叠的 条评论
为什么被折叠?



