最近忙着其他的事都没怎么敲代码,拿道简单的题练练手
Problem Description
Password security is a tricky thing. Users prefer simple passwords that are easy to remember (like buddy), but such passwords are often insecure. Some sites use random computer-generated passwords (like xvtpzyo), but users have a hard time remembering them and sometimes leave them written on notes stuck to their computer. One potential solution is to generate “pronounceable” passwords that are relatively secure but still easy to remember.
FnordCom is developing such a password generator. You work in the quality control department, and it’s your job to test the generator and make sure that the passwords are acceptable. To be acceptable, a password must satisfy these three rules:
It must contain at least one vowel.
It cannot contain three consecutive vowels or three consecutive consonants.
It cannot contain two consecutive occurrences of the same letter, except for ‘ee’ or ‘oo’.
(For the purposes of this problem, the vowels are ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’; all other letters are consonants.) Note that these rules are not perfect; there are many common/pronounceable words that are not acceptable.
Input
The input consists of one or more potential passwords, one per line, followed by a line containing only the word ‘end’ that signals the end of the file. Each password is at least one and at most twenty letters long and consists only of lowercase letters.
Output
For each password, output whether or not it is acceptable, using the precise format shown in the example.
Sample Input
a
tv
ptoui
bontres
zoggax
wiinq
eep
houctuh
end
Sample Output
is acceptable.
is not acceptable.
is not acceptable.
is not acceptable.
is not acceptable.
is not acceptable.
is acceptable.
is acceptable.
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
while(sc.hasNextLine())
{
if(s.equals("end"))
{
break;
}
else
{//Each password is at least one and at most twenty letters long
//and consists only of lowercase letters.
if(isContainsConVowel(s))
{
System.out.println("<"+s+"> "+"is not acceptable.");
}
else if(!isContainsVowel(s))
{
System.out.println("<"+s+"> "+"is not acceptable.");
}
else if(!isContainsTwo(s))
{
System.out.println("<"+s+"> "+"is not acceptable.");
}
else
{
System.out.println("<"+s+"> "+"is acceptable.");
}
}
s=sc.next();
}
}
/**
* 判断是否含有元音
* @param s
* @return
*/
static boolean isContainsVowel(String s)
{
//It must contain at least one vowel.
boolean isContainsVowel=false;
for (int i = 0; i < s.length(); i++) {
switch(s.charAt(i))
{
case 'a':
isContainsVowel=true;
break;
case 'e':
isContainsVowel=true;
break;
case 'i':
isContainsVowel=true;
break;
case 'o':
isContainsVowel=true;
break;
case 'u':
isContainsVowel=true;
break;
default:
isContainsVowel=false;
}
if(isContainsVowel)
{
return true;
}
}
return isContainsVowel;
}
/**
* 判断是否有3个连续的元音
* @param s
* @return
*/
static boolean isContainsConVowel(String s)
{
//It cannot contain three consecutive vowels or three consecutive consonants.
int conVowel=3;
int conConsonants=3;
boolean isconsecutiveVowels=false;
boolean isconsecutiveCons=false;
boolean istrue=false;
for (int i = 0; i < s.length(); i++) {
switch(s.charAt(i))
{
case 'a':
if(i!=0)
{
if(isconsecutiveVowels)
{
conVowel--;
if(conVowel==0)
{//有3个连续元音出现
istrue=true;
break;
}
}
else
{
conVowel=3;
conVowel--;
}
}
else
{
conVowel--;
}
isconsecutiveVowels=true;
isconsecutiveCons=false;
break;
case 'e':
if(i!=0)
{
if(isconsecutiveVowels)
{
conVowel--;
if(conVowel==0)
{//有3个连续元音出现
istrue=true;
break;
}
}
else
{
conVowel=3;
conVowel--;
}
}
else
{
conVowel--;
}
isconsecutiveVowels=true;
isconsecutiveCons=false;
break;
case 'i':
if(i!=0)
{
if(isconsecutiveVowels)
{
conVowel--;
if(conVowel==0)
{//有3个连续元音出现
istrue=true;
break;
}
}
else
{
conVowel=3;
conVowel--;
}
}
else
{
conVowel--;
}
isconsecutiveVowels=true;
isconsecutiveCons=false;
break;
case 'o':
if(i!=0)
{
if(isconsecutiveVowels)
{
conVowel--;
if(conVowel==0)
{//有3个连续元音出现
istrue=true;
break;
}
}
else
{
conVowel=3;
conVowel--;
}
}
else
{
conVowel--;
}
isconsecutiveVowels=true;
isconsecutiveCons=false;
break;
case 'u':
if(i!=0)
{
if(isconsecutiveVowels)
{
conVowel--;
if(conVowel==0)
{//有3个连续元音出现
istrue=true;
break;
}
}
else
{
conVowel=3;
conVowel--;
}
}
else
{
conVowel--;
}
isconsecutiveVowels=true;
isconsecutiveCons=false;
break;
default:
if(i!=0)
{
if(isconsecutiveCons)
{
conConsonants--;
if(conConsonants==0)
{//有3个连续辅音出现
istrue=true;
break;
}
}
else
{
conConsonants=3;
conConsonants--;
}
}
else
{
conConsonants--;
}
isconsecutiveCons=true;
isconsecutiveVowels=false;
}
if(istrue)
{
return true;
}
}
return false;
}
/**
* 判断是否有连续重复的字母其中ee,oo除外
* @param s
* @return
*/
static boolean isContainsTwo(String s)
{
for (int i = 0; i < s.length(); i++) {
if(i!=0)
{
if(s.charAt(i)==s.charAt(i-1))
{
if(s.charAt(i)!='e'&&s.charAt(i)!='o')
{
return false;
}
}
}
}
return true;
}
}