1. 程式人生 > >PAT——1043. 輸出PATest

PAT——1043. 輸出PATest

level ner arraylist ann char 要求 basic line 遍歷

給定一個長度不超過10000的、僅由英文字母構成的字符串。請將字符重新調整順序,按“PATestPATest....”這樣的順序輸出,並忽略其它字符。當然,六種字符的個數不一定是一樣多的,若某種字符已經輸出完,則余下的字符仍按PATest的順序打印,直到所有字符都被輸出。

輸入格式:

輸入在一行中給出一個長度不超過10000的、僅由英文字母構成的非空字符串。

輸出格式:

在一行中按題目要求輸出排序後的字符串。題目保證輸出非空。

輸入樣例:

redlesPayBestPATTopTeePHPereatitAPPT

輸出樣例:

PATestPATestPTetPTePePee

 1 package com.hone.basical;
2 3 import java.util.Scanner; 4 import java.util.ArrayList; 5 import java.util.List; 6 /** 7 * 原題目:https://www.patest.cn/contests/pat-b-practise/1043 8 * @author Xia 9 *思路:遍歷所有的字符,然後用list存儲起來 再一次又一次遍歷Patest直到所有的都為零。 10 */ 11 12 public class basicalLevel1043printfPatTest { 13 14 public static
void main(String[] args) { 15 16 Scanner in = new Scanner(System.in); 17 String string = in.nextLine(); 18 in.close(); 19 20 List<Character> PList = new ArrayList<>(); 21 List<Character> AList = new ArrayList<>(); 22 List<Character> TList = new
ArrayList<>(); 23 List<Character> eList = new ArrayList<>(); 24 List<Character> sList = new ArrayList<>(); 25 List<Character> tList = new ArrayList<>(); 26 27 for (int i = 0; i < string.length(); i++) { 28 switch (string.charAt(i)) { 29 case ‘P‘: 30 PList.add(string.charAt(i)); 31 break; 32 case ‘A‘: 33 AList.add(string.charAt(i)); 34 break; 35 case ‘T‘: 36 TList.add(string.charAt(i)); 37 break; 38 case ‘e‘: 39 eList.add(string.charAt(i)); 40 break; 41 case ‘s‘: 42 sList.add(string.charAt(i)); 43 break; 44 case ‘t‘: 45 tList.add(string.charAt(i)); 46 break; 47 48 } 49 } 50 51 int maxSize = PList.size(); 52 if (AList.size() > maxSize) { 53 maxSize = AList.size(); 54 } 55 if (TList.size() > maxSize) { 56 maxSize = TList.size(); 57 } 58 if (eList.size() > maxSize) { 59 maxSize = eList.size(); 60 } 61 if (sList.size() > maxSize) { 62 maxSize = sList.size(); 63 } 64 if (tList.size() > maxSize) { 65 maxSize = tList.size(); 66 } 67 for (int i = 0; i < maxSize; i++) { 68 69 if (i < PList.size()) { 70 System.out.print(PList.get(i)); 71 } 72 if (i < AList.size()) { 73 System.out.print(AList.get(i)); 74 } 75 if (i < TList.size()) { 76 System.out.print(TList.get(i)); 77 } 78 if (i < eList.size()) { 79 System.out.print(eList.get(i)); 80 } 81 if (i < sList.size()) { 82 System.out.print(sList.get(i)); 83 } 84 if (i < tList.size()) { 85 System.out.print(tList.get(i)); 86 } 87 } 88 } 89 }

PAT——1043. 輸出PATest