PAT乙级41到60题练习代码全纪录
1041 考试座位号
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine().trim());
String[] info = new String[n + 1]; // n个考生的信息,为了便于查找初始化n+1个
for(int i = 0; i < n; i++) {
String[] temp = br.readLine().trim().split("\\s+");
int testPos = Integer.parseInt(temp[1]); // 试机座位号
info[testPos] = temp[0] + " " + temp[2]; // 试机座位号对应的其他信息
}
int m = Integer.parseInt(br.readLine().trim());
int[] test = new int[m]; // m个待查询试机座位号
String[] temp = br.readLine().trim().split("\\s+");
for(int j = 0; j < m; j++) {
test[j] = Integer.parseInt(temp[j]);
}
for(int j = 0; j < m; j++) {
System.out.println(info[test[j]]);
}
}
}
1042 字符统计
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine().trim().toLowerCase();
Character ch; // 当前字符
int max = 0; // 最大字频
int[] count = new int[26]; // 字母字频统计
for (int i = 0; i < str.length(); i++) {
ch = str.charAt(i);
if (Character.isLetter(ch)) {
int index = ch - 'a';
count[index]++;
if (count[index] > max)
max = count[index];
}
}
/* 按照字母顺序输出第一个字频最大的 */
for (int i = 0; i < 26; i++) {
if (count[i] == max) {
System.out.println((char) (i + 'a') + " " + max);
break;
}
}
}
}
1043 输出PATest
方法一:
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine().trim();
int[] count = new int[6]; // 对PATest的每个字母进行计数
String sample = "PATest";
for (int i = 0; i < str.length(); i++) {
for (int j = 0; j < 6; j++) {
if (str.charAt(i) == sample.charAt(j)) {
count[j]++;
}
}
}
int total = count[0] + count[1] + count[2] + count[3] + count[4] + count[5];
while (total > 0) {
// 直到全部输出
for (int i = 0; i < 6; i++) {
if (count[i] > 0) {
count[i]--;
total--;
System.out.print(sample.charAt(i));
}
}
}
}
}
方法二:
import java.io.InputStreamReader;
import java.util.HashMap;
import java.io.BufferedReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine().trim();
String sample = "PATest";
HashMap<Character, Integer> count = new HashMap<>();
for (int i = 0; i < 6; i++) {
count.put(sample.charAt(i), 0); // 初始化
}
for (int i = 0; i < str.length(); i++) {
count.computeIfPresent(str.charAt(i), (k, v) -> v + 1); // 计数
}
Character key;
int total = count.get('P') + count.get('A') + count.get('T') + count.get('e') + count.get('s') + count.get('t');
while (total > 0) {
for (int i = 0; i < 6; i++) {
key = sample.charAt(i);
if (count.get(key) > 0) {
System.out.print(key);
total--;
}
count.computeIfPresent(key, (k, v) -> v - 1); // 输出
}
}
}
}
1044 火星数字
唯一的坑就是!13、26这样的十进制,写成十三进制是10、20,但是火星文是tam、hel,也就是说,个位的0不需要表示出来。
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class Main {
static String[] sample1 = {
"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" }; // 低位数字
static String[] sample2 = {
"tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" }; // 高位数字
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine().trim());
String[] words = new String[n];
String[] trans = new String[n];
for (int i = 0; i < n; i++) {
words[i] = br.readLine().trim();
if (Character.isLetter(words[i].charAt(0))) {
// true则是火星文
trans[i] = toEarth(words[i]);
} else {
trans[i] = toMartian(words[i]);
}
}
for (int i = 0; i < n; i++) // 输出结果
System.out.println(trans[i]);
}
public static String toMartian(String s) {
// 数字转火星文
StringBuilder mars = new StringBuilder("");
int earth = Integer.parseInt(s);
if (earth == 0) {
mars.append("tret");
} else {
int temp1 = earth % 13; // 低位
int temp2 = (earth / 13) % 13; // 高位
if (temp2 > 0)
mars.append(sample2[temp2]);
if(temp1 != 0 && temp2 != 0)
mars.append(" ");
if (temp1 > 0) // 低位为零时不输出
mars.append(sample1[temp1]);
}
return mars.toString();
}
public static String toEarth(String s) {
// 火星文转数字
int earth = 0;
String[] temp = s.split("\\s+");
if (temp[0] != "tret") {
for (int i = 0; i < temp.length; i++) {
for (int j = 1; j < 13; j++) {
if (temp[i].equals(sample1[j]))
earth += j;
if (temp[i].equals(sample2[j]))
earth += j * 13;
}
}
}
return String.valueOf(earth);
}
}
1045 快速排序(1、3、4、5测试点运行超时)
本题java劝退,典型的考效率的,时间复杂度不能超过O(n),解决问题本身很简单。另外,坑点注意!测试点2是主元为0的情况,要输出 “0\n\n” 格式才正确。
思路一(较慢,只能过测试点0):
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.valueOf(br.readLine().trim());
String[] temp =</