PAT-BASIC1081——检查密码
我的PAT-BASIC代码仓:https://github.com/617076674/PAT-BASIC
原题链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805261217153024
题目描述:
知识点:字符串
思路:按题述编程即可
时间复杂度是O(n),其中n为输入字符串的长度。空间复杂度是O(1)。
C++代码:
#include<iostream>
#include<string>
using namespace std;
bool containsIllegalChar(string s);
bool containsNum(string s);
bool containsLetter(string s);
int main(){
int N;
cin >> N;
getchar();
string password;
for(int i = 0; i < N; i++){
getline(cin, password);
if(password.length() < 6){
printf("Your password is tai duan le.\n");
}else if(containsIllegalChar(password)){
printf("Your password is tai luan le.\n");
}else if(containsLetter(password) && !containsNum(password)){
printf("Your password needs shu zi.\n");
}else if(!containsLetter(password) && containsNum(password)){
printf("Your password needs zi mu.\n");
}else{
printf("Your password is wan mei.\n");
}
}
return 0;
}
bool containsIllegalChar(string s){
for(int i = 0; i < s.length(); i++){
if(!(s[i] >= 'a' && s[i] <= 'z') && !(s[i] >= 'A' && s[i] <= 'Z') && !(s[i] >= '0' && s[i] <= '9') && s[i] != '.'){
return true;
}
}
return false;
}
bool containsNum(string s){
for(int i = 0; i < s.length(); i++){
if(s[i] >= '0' && s[i] <= '9'){
return true;
}
}
return false;
}
bool containsLetter(string s){
for(int i = 0; i < s.length(); i++){
if((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')){
return true;
}
}
return false;
}
C++解题报告: