【PAT甲级 火星数字】1100 Mars Numbers (20 分)Java 全部AC
生活随笔
收集整理的這篇文章主要介紹了
【PAT甲级 火星数字】1100 Mars Numbers (20 分)Java 全部AC
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目
提交Java的時候,千萬不要寫第一行的包名稱!!被這個bug折騰了一個小時。。
題解1:Java
import java.util.ArrayList; import java.util.List; import java.util.Scanner;public class Main {static String marsGe[] = { "tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov","dec" };static String marsShi[] = { "tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer","jou" };// 地球個位轉火星static String geToMars(int ge) {return marsGe[ge];}// 地球十位轉火星static String shiToMars(int shi) {return marsShi[shi];}// 火星轉地球個位static int marsToge(String ge) {for (int i = 0; i <= 12; i++) {if (marsGe[i].equals(ge))return i;}return -1;}// 火星轉地球十位static int marsToshi(String shi) {for (int i = 0; i <= 12; i++) {if (marsShi[i].equals(shi))return i;}return -1;}// 地球轉火星static String toMars(String str) {int num = Integer.parseInt(str);int ge = num % 13;// 個位int shi = num / 13;// 十位if (shi == 0) {return geToMars(ge);} else if (ge == 0) {return shiToMars(shi).trim();} else {return shiToMars(shi) + " " + geToMars(ge);}}// 火星轉地球static int toEarth(String str) {String[] arr = null;String shi;String ge;if (str.length() > 5) {// 兩個單詞arr = str.split(" ");shi = arr[0];ge = arr[1];return marsToshi(shi) * 13 + marsToge(ge);} else {// 一個單詞 可能是個位或者十位ge = str;if (marsToge(ge) != -1) {// 不是個位return marsToge(ge);} else {return marsToshi(ge) * 13;}}}public static void main(String[] args) {Scanner sc = new Scanner(System.in);// 總數int total = Integer.parseInt(sc.nextLine());// 讀取String[] nums = new String[500];for (int i = 0; i < total; i++) {String line = sc.nextLine();nums[i] = line;}// 轉化List<String> res = new ArrayList<>();for (int i = 0; i < total; i++) {if (nums[i].charAt(0) >= '0' && nums[i].charAt(0) <= '9') {// 第一個字符是數字還是字母res.add(toMars(nums[i]));} else {res.add(toEarth(nums[i]) + "");}}// 輸出for (String s : res) {System.out.println(s);}} }所有輸入
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168所有輸出
tret jan feb mar apr may jun jly aug sep oct nov dec tam tam jan tam feb tam mar tam apr tam may tam jun tam jly tam aug tam sep tam oct tam nov tam dec hel hel jan hel feb hel mar hel apr hel may hel jun hel jly hel aug hel sep hel oct hel nov hel dec maa maa jan maa feb maa mar maa apr maa may maa jun maa jly maa aug maa sep maa oct maa nov maa dec huh huh jan huh feb huh mar huh apr huh may huh jun huh jly huh aug huh sep huh oct huh nov huh dec tou tou jan tou feb tou mar tou apr tou may tou jun tou jly tou aug tou sep tou oct tou nov tou dec kes kes jan kes feb kes mar kes apr kes may kes jun kes jly kes aug kes sep kes oct kes nov kes dec hei hei jan hei feb hei mar hei apr hei may hei jun hei jly hei aug hei sep hei oct hei nov hei dec elo elo jan elo feb elo mar elo apr elo may elo jun elo jly elo aug elo sep elo oct elo nov elo dec syy syy jan syy feb syy mar syy apr syy may syy jun syy jly syy aug syy sep syy oct syy nov syy dec lok lok jan lok feb lok mar lok apr lok may lok jun lok jly lok aug lok sep lok oct lok nov lok dec mer mer jan mer feb mer mar mer apr mer may mer jun mer jly mer aug mer sep mer oct mer nov mer dec jou jou jan jou feb jou mar jou apr jou may jou jun jou jly jou aug jou sep jou oct jou nov jou dec題解2:特別傻的解法 Java
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner;public class Main {// tret,jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, decstatic String geToMars(int ge) {switch (ge) {case 0:return "tret";case 1:return "jan";case 2:return "feb";case 3:return "mar";case 4:return "apr";case 5:return "may";case 6:return "jun";case 7:return "jly";case 8:return "aug";case 9:return "sep";case 10:return "oct";case 11:return "nov";case 12:return "dec";default:return "GeEXCEPTION!";}}// tret,tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, joustatic String shiToMars(int shi) {switch (shi) {case 0:return "";case 1:return "tam ";case 2:return "hel ";case 3:return "maa ";case 4:return "huh ";case 5:return "tou ";case 6:return "kes ";case 7:return "hei ";case 8:return "elo ";case 9:return "syy ";case 10:return "lok ";case 11:return "mer ";case 12:return "jou ";default:return "shiEXCEPTION!";}}// tret,jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, decstatic int marsToge(String ge) {switch (ge) {case "tret":return 0;case "jan":return 1;case "feb":return 2;case "mar":return 3;case "apr":return 4;case "may":return 5;case "jun":return 6;case "jly":return 7;case "aug":return 8;case "sep":return 9;case "oct":return 10;case "nov":return 11;case "dec":return 12;default:return -1;}}// tret,tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, joustatic int marsToshi(String ge) {switch (ge) {case "tret":return 0;case "tam":return 1;case "hel":return 2;case "maa":return 3;case "huh":return 4;case "tou":return 5;case "kes":return 6;case "hei":return 7;case "elo":return 8;case "syy":return 9;case "lok":return 10;case "mer":return 11;case "jou":return 12;default:return -2;}}// 地球轉火星static String toMars(String str) {int num = Integer.parseInt(str);int ge = num % 13;// 個位int shi = num / 13;// 十位if (shi == 0) {return geToMars(ge);} else if (ge == 0) {return shiToMars(shi).trim();} else {return shiToMars(shi) + geToMars(ge);}}// 火星轉地球static int toEarth(String str) {String[] arr = null;String shi;String ge;if (str.length() > 5) {arr = str.split(" ");shi = arr[0];ge = arr[1];return marsToshi(shi) * 13 + marsToge(ge);} else {ge = str;if (marsToge(ge) != -1) {return marsToge(ge);} else {return marsToshi(ge) * 13;}}}public static void main(String[] args) {Scanner sc = new Scanner(System.in);// 總數int total = Integer.parseInt(sc.nextLine());// 讀取int cnt = 0;String[] nums = new String[500];for (int i = 0; i < total; i++) {String line = sc.nextLine();nums[cnt] = line;cnt++;}// 轉化List<String> res = new ArrayList<>();for (int i = 0; i < total; i++) {if (nums[i].charAt(0) >= '0' && nums[i].charAt(0) <= '9') {// 第一個字符是數字還是字母res.add(toMars(nums[i]));} else {res.add(toEarth(nums[i]) + "");}}// 輸出for (String s : res) {System.out.println(s);}} }總結
以上是生活随笔為你收集整理的【PAT甲级 火星数字】1100 Mars Numbers (20 分)Java 全部AC的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【PAT甲级 排列组合】1093 Cou
- 下一篇: 【算法设计与分析】Dijskra算法代码