1081-检查密码(PAT-B)

20 篇文章 0 订阅

题目如下:

本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能。该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母、数字和小数点 .,还必须既有字母也有数字。
输入格式:

输入第一行给出一个正整数 N(≤ 100),随后 N 行,每行给出一个用户设置的密码,为不超过 80 个字符的非空字符串,以回车结束。
输出格式:

对每个用户的密码,在一行中输出系统反馈信息,分以下5种:
如果密码合法,输出Your password is wan mei.;
如果密码太短,不论合法与否,都输出Your password is tai duan le.;
如果密码长度合法,但存在不合法字符,则输出Your password is tai luan le.;
如果密码长度合法,但只有字母没有数字,则输出Your password needs shu zi.;
如果密码长度合法,但只有数字没有字母,则输出Your password needs zi mu.。
输入样例:

5
123s
zheshi.wodepw
1234.5678
WanMei23333
pass*word.6
输出样例:

Your password is tai duan le.
Your password needs shu zi.
Your password needs zi mu.
Your password is wan mei.
Your password is tai luan le.

解决方案:

1.思路:本题的题意并不复杂,按照题意分别进行判断即可。只是在第一次提交的时候并没有通过,后来上网查看大佬博客后才知道题目没说密码不含空格,所以要用getline,而不能使用cin,因为使用了getline,所以应该用getchar读到N后的空格。
2.按照条件进行判断:
遍历一遍string字符串,将字符串中的数字,字母,小数点和其他字符找出来,用1表示字母,用2表示数字,用3表示小数点,用4表示其他字符,接着将这些数字保存在一个数组中,然后用sort函数对数组进行递增的排序
这样就方便判断了:

1. 首先判断数组的最后一位;如果数组的最后一位(最大的元素)是4,这就说明字符串里面有不合法的字符。
2. 其次判断数组的第一位;如果数组的第一位(最小的元素)是2,这就说明字符串里面没有字母。
3. 如果数组的第一位是1,最后一位是2,这说明字符串里只有数字和字母,就代表着这个密码是完美的。
4. 如果数组的最后一位是1,这说明字符串里只有字母。

当然,上述的第四种情况只是说字符串里只有字母,没有数字和小数点,但是存在一种这样的情况,那就是字符串中只有字母和小数点,没有数字,因为字母是1,小数点是3;这样的话排序就不好使了。不过目前没有想出新的解决方案,只好老老实实的遍历数组,遇到2的时候就终止循环,输出“完美”,如果循环执行完毕,说明没有数字,就输出“需要数字”。
用输入样例来举例:

 1.     123s      长度不满足,直接pass掉。
 2.     zheshi.wodepw
 用数组标记后为:1 1 1 1 1 1 3 1 1 1 1 1 1   这便是那一种特殊的情况,需要遍历来寻找是否有数字。
 3.     1234.5678
 用数组标记后为:2 2 2 2 3 2 2 2 2
 排序后:2 2 2 2 2 2 2 2 3
 第一位是2,说明没有字母。
 4.     WanMei23333
 用数组标记后为:1 1 1 1 1 1 2 2 2 2 2
 排序后为:1 1 1 1 1 1 2 2 2 2 2
 第一位是1,最后一位是2,说明字符串只有数字和字母,密码完美。
 5.     pass*word.6
 用数组标记后为:1 1 1 1 4 1 1 1 1 3 2
 排序后为:1 1 1 1 1 1 1 1 2 3 4
 最后一位是4,说明有不合法字符。

按照以上思路,就可以写出代码了:

void Judge(string str, int s)
{
	int jud[80]; 
	for(int i = 0; i < s; i++) {
		if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) jud[i] = 1;  //1代表字母
		else if (str[i] >= '0' && str[i] <= '9') jud[i] = 2;  //2代表数字
		else if (str[i] == '.') jud[i] = 3;  //3代表“.”
		else jud[i] = 4;  //4代表其他的字符 
	}
	sort(jud, jud + s);  //从小到大排序 
	if(jud[s - 1] == 4) cout << "Your password is tai luan le." << endl;  //存在不合法字符
	else if(jud[0] == 2) cout << "Your password needs zi mu." << endl;  //最小的是2说明没有字母
	else if(jud[0] == 1 && jud[s - 1] == 2) cout << "Your password is wan mei." << endl;  //只有数字和字母说明肯定完美啊 
	//还有一种情况,那就是没有数字
	int flag = 0;
	if(jud[s - 1] == 1) cout << "Your password needs shu zi." << endl;  //只有字母
	else if(jud[0] == 1 && jud[s - 1] == 3) {
		for(int i = 0; i < s; i++) {
			if(jud[i] == 2) {
				cout << "Your password is wan mei." << endl;  //如果有数字,说明完美 
				break;
			}
			flag++;
		}
		if(flag == s) cout << "Your password needs shu zi." << endl;  //如果循环执行完了,说明没有数字 
	}  
}

完整代码:

#include <iostream>
#include <algorithm> 
using namespace std;
void Judge(string str, int s)
{
	int jud[80]; 
	for(int i = 0; i < s; i++) {
		if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) jud[i] = 1;  
		else if (str[i] >= '0' && str[i] <= '9') jud[i] = 2;  
		else if (str[i] == '.') jud[i] = 3;  
		else jud[i] = 4;   
	}
	sort(jud, jud + s);  
	if(jud[s - 1] == 4) cout << "Your password is tai luan le." << endl;  
	else if(jud[0] == 2) cout << "Your password needs zi mu." << endl;  
	else if(jud[0] == 1 && jud[s - 1] == 2) cout << "Your password is wan mei." << endl;  
	int flag = 0;
	if(jud[s - 1] == 1) cout << "Your password needs shu zi." << endl;  
	else if(jud[0] == 1 && jud[s - 1] == 3) {
		for(int i = 0; i < s; i++) {
			if(jud[i] == 2) {
				cout << "Your password is wan mei." << endl;  
				break;
			}
			flag++;
		}
		if(flag == s) cout << "Your password needs shu zi." << endl;  
	}  
}
int main()
{
	int N;
	cin >> N;
	getchar();
	string str;
	int s;
	for(int i = 0; i < N; i++) {
		//cin >> str;
		getline(cin, str);
		s = str.length();
		if(s < 6) cout << "Your password is tai duan le." << endl;  //密码太短 
		else Judge(str, s);
	}
}

大神的解答:

看了柳神的代码和解析,发现自己的方法好繁琐。。。
柳神的解析:PAT 1081. 检查密码 (15) - 乙级
附柳神的代码:

#include <iostream>
#include <cctype>
using namespace std;
int main() {
    int n;
    cin >> n; getchar();
    for (int i = 0; i < n; i++) {
        string s;
        getline(cin, s);
        if (s.length() >= 6) {
            int invalid = 0, hasAlpha = 0, hasNum = 0;
            for (int j = 0; j < s.length(); j++) {
                if (s[j] != '.' && !isalnum(s[j])) invalid = 1;
                else if (isalpha(s[j])) hasAlpha = 1;
                else if (isdigit(s[j])) hasNum = 1;
            }
            if (invalid == 1) cout << "Your password is tai luan le.\n";
            else if (hasNum == 0) cout << "Your password needs shu zi.\n";
            else if (hasAlpha == 0) cout << "Your password needs zi mu.\n";
            else cout << "Your password is wan mei.\n";
        } else
            cout << "Your password is tai duan le.\n";
    }
    return 0;
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值