1003 我要通过
1003?我要通過(guò)!?(20?分) 字符串中必須僅有?P、?A、?T這三種字符,不可以包含其它字符; 任意形如?xPATx?的字符串都可以獲得“答案正確”,其中?x?或者是空字符串,或者是僅由字母?A?組成的字符串; 如果?aPbTc?是正確的,那么?aPbATca?也是正確的,其中?a、?b、?c?均或者是空字符串,或者是僅由字母?A?組成的字符串。
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner in =new Scanner(System.in);int num =in.nextInt();String []result=new String[num];for(int i =0;i<num;i++){String testStr=in.next();result[i]=myMethod(testStr);}for(String s:result){System.out.println(s);}}private static String myMethod(String s) {// TODO Auto-generated method stubchar[]tempChar=s.toCharArray();int countP=0;int countA=0;int countT=0;String result="";for (int i =0;i<tempChar.length;i++){if(tempChar[i]!='P'&&tempChar[i]!='A'&&tempChar[i]!='T'){break;} else if(tempChar[i]=='P'){countP++;}else if(tempChar[i]=='A'){countA++;}else if(tempChar[i]=='T'){countT++;}if(countP==2||countT==2){break;}}if(countP==1&&countT==1&&countA!=0){int indexP=s.indexOf('P');int indexA=s.indexOf('A');int indexT=s.indexOf('T');if((indexP)*(indexT-indexP-1)==(s.length()-indexT-1)){result="YES";}else{result="NO";}}else{result="NO";}return result;}}
“答案正確”是自動(dòng)判題系統(tǒng)給出的最令人歡喜的回復(fù)。本題屬于 PAT 的“答案正確”大派送 —— 只要讀入的字符串滿足下列條件,系統(tǒng)就輸出“答案正確”,否則輸出“答案錯(cuò)誤”。
得到“答案正確”的條件是:
現(xiàn)在就請(qǐng)你為 PAT 寫一個(gè)自動(dòng)裁判程序,判定哪些字符串是可以獲得“答案正確”的。
輸入格式:
每個(gè)測(cè)試輸入包含 1 個(gè)測(cè)試用例。第 1 行給出一個(gè)正整數(shù)?n?(<),是需要檢測(cè)的字符串個(gè)數(shù)。接下來(lái)每個(gè)字符串占一行,字符串長(zhǎng)度不超過(guò) 100,且不包含空格。
輸出格式:
每個(gè)字符串的檢測(cè)結(jié)果占一行,如果該字符串可以獲得“答案正確”,則輸出?YES,否則輸出?NO。
輸入樣例:
8 PAT PAAT AAPATAA AAPAATAAAA xPATx PT Whatever APAAATAA輸出樣例:
YES YES YES YES NO NO NO NOimport java.util.Scanner;public class Main {public static void main(String[] args) {Scanner in =new Scanner(System.in);int num =in.nextInt();String []result=new String[num];for(int i =0;i<num;i++){String testStr=in.next();result[i]=myMethod(testStr);}for(String s:result){System.out.println(s);}}private static String myMethod(String s) {// TODO Auto-generated method stubchar[]tempChar=s.toCharArray();int countP=0;int countA=0;int countT=0;String result="";for (int i =0;i<tempChar.length;i++){if(tempChar[i]!='P'&&tempChar[i]!='A'&&tempChar[i]!='T'){break;} else if(tempChar[i]=='P'){countP++;}else if(tempChar[i]=='A'){countA++;}else if(tempChar[i]=='T'){countT++;}if(countP==2||countT==2){break;}}if(countP==1&&countT==1&&countA!=0){int indexP=s.indexOf('P');int indexA=s.indexOf('A');int indexT=s.indexOf('T');if((indexP)*(indexT-indexP-1)==(s.length()-indexT-1)){result="YES";}else{result="NO";}}else{result="NO";}return result;}}
?
轉(zhuǎn)載于:https://www.cnblogs.com/xiaozhushifu/p/10845780.html
總結(jié)
- 上一篇: Windows Azure HandBo
- 下一篇: 使用Json让Java和C#沟通的方法