题意:输入多个字符串,然后逐个判断是否符合要求。
规则一:密码中至少包含一个元音字母;
规则二:它不能包含三个连续的元音或者三个连续的辅音;
规则三:它不能连续出现相同的字母,除了“ee”或“oo”。
import java.util.Scanner;
public class Main {
public static char[] chs = { 'a', 'e', 'i', 'o', 'u' };
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s = sc.next();
if (s.equals("end")) {
return;
}
boolean flag = isAcceptable(s);// 不能改变s的值
s = ("<" + s + ">");
if (flag) {
s += " is acceptable.";
} else {
s += " is not acceptable.";
}
System.out.println(s);
}
}
public static boolean isAcceptable(String str) {
if (!isFirstRule(str)) {
return false;
}
if (!isSecondRule(str)) {
return false;
}
if (!isThirdRule(str)) {
return false;
}
return true;
}
public static boolean isFirstRule(String s) {
for (int i = 0; i < s.length(); i++) {
if (isVowel(s, i)) {
return true;
}
}
return false;
}
public static boolean isVowel(String s, int i) {// 判断是不是元音
for (int j = 0; j < chs.length; j++) {
if (s.charAt(i) == chs[j]) {
return true;
}
}
return false;
}
public static boolean isSecondRule(String s) {
for (int i = 0; i < s.length() - 2; i++) {
boolean flag = false;
if (isVowel(s, i)) {
flag = true;
}
// 如果flag是true,那么后面的字母必须全为元音才返回true;
// 如果flag是false,那么后面的字母必须全为辅音才返回true;
if (flag == isVowel(s, i + 1)) {
if (flag == isVowel(s, i + 2)) {
return false;// 三个元音或者三个辅音
}
}
}
return true;
}
public static boolean isThirdRule(String s) {
for (int i = 0; i < s.length() - 1; i++) {
char ch1 = s.charAt(i);
char ch2 = s.charAt(i + 1);
boolean flag = false;
if (ch1 == 'e' || ch1 == 'o') {
flag = true;
}
if (ch1 == ch2) {// 当ch1与ch2是元音时返回true,否则返回false
return flag;
}
}
return true;// 当ch1ch2不相等时,返回true;
}
}
a tv ptoui bontres zoggax wiinq eep houctuh end
<a> is acceptable. <tv> is not acceptable. <ptoui> is not acceptable. <bontres> is not acceptable. <zoggax> is not acceptable. <wiinq> is not acceptable. <eep> is acceptable. <houctuh> is acceptable.
hdu 1039 Easier Done Than Said?
原文地址:http://blog.csdn.net/hncu1306602liuqiang/article/details/46656231