PAT 乙级 1081 检查密码
1. 题目简述及在线测试位置
1.1 密码的规则是:不少于6个字符;密码包含的字符范围是 英文字母、数字和小数点;密码中必须要有数字和字母。据此检查输入的密码是否满足要求并打印对应的提示
1.2 在线测试位置: 1081 检查密码
2. 基本思路
2.1 待检查的密码中可能包含空格,因此通过getline()函数接收输入信息
2.2 字符串基础题,根据题意编码即可
3. 完整AC代码
#include <string>
#include <iostream>
using namespace std;
#define Length 6
int main()
{
int N; string Str;
bool AFlag, NumFlag; //是否有字母 是否有数字
cin >> N;
getline(cin, Str); //处理缓冲区中的回车符
while (N--)
{
AFlag = 0, NumFlag = 0;
getline(cin, Str); //输入可能有空格,因此使用getline()
if (Str.length() < Length)
{
cout << "Your password is tai duan le." << endl;
continue;
}
int i;
for (i = 0; i < Str.length(); i++)
{
if ((Str[i] >= 'A' && Str[i] <= 'Z') || (Str[i] >= 'a' && Str[i] <= 'z'))
AFlag = 1;
else if (Str[i] >= '0' && Str[i] <= '9')
NumFlag = 1;
else if (Str[i] == '.');
else
{
cout << "Your password is tai luan le." << endl;
break;
}
}
if (i < Str.length()) //包含其他字符
continue;
else
{
if (AFlag && NumFlag)
cout << "Your password is wan mei." << endl;
else if (!AFlag && NumFlag)
cout << "Your password needs zi mu." << endl;
else if (AFlag && !NumFlag)
cout << "Your password needs shu zi." << endl;
}
}
return 0;
}