1. 程式人生 > >字串處理:多條件篩選

字串處理:多條件篩選

杭電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;
	}
}