字符串处理:多条件筛选

杭电1039:题目大意:输入一串小写字母,检查是否能够作为安全的密码使用。
能作为安全密码的字符串符合三个条件:
1.它必须包含至少一个元音。            
2.它不能包含三个连续的元音或三个连续的辅音。            
3.它不能包含两个连续的相同的字母,除了“EE”或“oo”。
#include<iostream>
#include<string>
using namespace std;
bool fun1(string s){        //它必须包含至少一个元音。
	int n = 0;
	for ( n ; n < s.length(); n++)
		if (s[n] == 'a' || s[n] == 'e' || s[n] == 'i' || s[n] == 'o' || s[n] == 'u')
			return true;
		return false;
}
bool fun2(string s){       //它不能包含两个连续的相同的字母,除了“EE”或“oo”。
    int i =s.size();        //s.size()返回的是无符号整型,小余0时会溢出
	for (int n = 0; n <= i - 2; n++)
	{
		if (s[n] == s[n + 1])
            {
			if (s[n] == 'e' || s[n] == 'o')
				continue;

			return false;
            }
	}
	return true;
}
bool isvowels(char c){
	if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
		return true;
	else
		return false;
}
bool fun3(string s){        //它不能包含三个连续的元音或三个连续的辅音
	for (int n = 2; n < s.length(); n++)
		if (isvowels(s[n]) == isvowels(s[n - 1]) && isvowels(s[n - 1]) == isvowels(s[n - 2]))
			return false;
	return true;
}
int main(){
	string s;
	while (cin >> s&&s != "end"){
		if (fun1(s) && fun2(s) && fun3(s))
			cout << "<" << s << ">" << " is acceptable." << endl;
		else
			cout << "<" << s << ">" << " is not acceptable." << endl;
	}
}

一。每个函数单独考虑一个条件,不要一个函数考虑太多,最后判断用且就行了。
二。size_type是无符号整型,小于0时会溢出,多多注意.





杭电2043 题目大意:也是密码题目,输入一个数字n,代表n次测试,然后输入n串,检查是否能够作为安全的密码使用。
能作为安全密码的字符串符合下面的两个个条件:
(1).密码长度大于等于8,且不要超过16。
(2).密码中的字符应该来自下面“字符类别”中四组中的至少三组。

这四个字符类别分别为:
1.大写字母:A,B,C...Z;
2.小写字母:a,b,c...z;
3.数字:0,1,2...9;
4.特殊符号:~,!,@,#,$,%,^;


#include<iostream>
#include<string>
using namespace std;
bool fun1(string s){
	for (int n = 0; n < s.size(); n++){
		if (s[n] >= 'A'&&s[n] <= 'Z')
			return true;
	}
	return false;
}
bool fun2(string s){
	for (int n = 0; n < s.size(); n++){
		if (s[n] >= 'a'&&s[n] <= 'z')
			return true;
	}
	return false;
}
bool fun3(string s){
	for (int n = 0; n < s.size(); n++){
		if (s[n] >= '0'&&s[n] <= '9')
			return true;
	}
	return false;
}
bool fun4(string s){
	for (int n = 0; n < s.size(); n++){
		if (s[n] == '~' || s[n] == '!' || s[n] == '@' || s[n] == '#' || s[n] == '$' || s[n] == '^' || s[n] == '^'){
			return true;
		}
	}
	return false;
}
int main(){
	int t;
	string s;
	cin >> t;
	cin.ignore(1, '\n');
	while (t--){
		getline(cin, s);
		int a=0,b=0,c=0,d=0;
		if(fun1(s))
            a=1;
        if(fun2(s))
            b=1;
        if(fun3(s))
            c=1;
        if(fun4(s))
            d=1;
		if (s.size() >= 8 && s.size() < 16){
			if ((a+b+c+d)>=3){
				cout << "YES" << endl;
			}
			else
				cout << "NO" << endl;
		}
		else
			cout << "NO" << endl;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值