1003. 我要通过!(20)

来源:互联网 发布:51talk怎么样 知乎 编辑:程序博客网 时间:2024/05/16 17:03
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


/**
 * “答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。
得到“答案正确”的条件是:
1. 字符串中必须仅有P, A, T这三种字符,不可以包含其它字符;
2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
3. 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a, b, c 均或者是空字符串,或者是仅由字母 A 组成的字符串。
 * */
public class Test1003 {
public static void main(String arg[]){
print();
}
public static void print(){
System.out.println("请输入:");
Scanner scanner = new Scanner(System.in);
try{
int i = scanner.nextInt();
List<String> list = new ArrayList<String>();
for(int m = 0;m < i; m++){
String n = scanner.next();
list.add(n);
}
for(String s:list){
result(s);
}
}catch(Exception e){
print();
System.out.println();
}
}
public static void result(String s){
String m = "A*PA+TA*";
String n = "PA+T";
if(s.matches(m)){
if(s.matches(n)){
System.out.println("YES");
}else {
String[] str = s.split("P|T");
int a = str[0].length();
int b = str[1].length();
int c = str[2].length();
if ((c - a) / a == (b - 1)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}else{
System.out.println("NO");
}

}

}
原创粉丝点击