自己报的名,含着泪也要考完试TUT
新人处子文预警!!博主乃一枚java小白,少不更事之时听闻PAT考出来方便找工作,于是也没多想就走上了这条不归路……
用java考要小心超时哦
你va在很多时候都相当于(C++)- -,这也是很多人喜欢她的原因。然而Java是解释型语言,相对于C/C++程序效率低、执行速度慢。在PAT这种有耗时限制的编程考试中,亲测你va的耗时一般是同质量c++的几十到百倍不等,时间限制在100-400ms不等,有时原本正确的也超时运行了(摊手),不过还好,大部分情况还是问题不大的。
另外一大坑大概是PAT的题目设定,有不少文字游戏嫌疑,主要考察的是逻辑思维以及解决问题的能力。然而名都报了含着泪也是要上考场的……
废话完了开正题(1到20题,无特殊备注则测试点全通过)
1001 害死人不偿命的(3n+1)猜想
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int count = 0;
while (n > 1) {
if (n % 2 == 0)
n = n / 2;
else
n = (n * 3 + 1) / 2;
count++;
}
System.out.println(count);
}
}
1002 写出这个数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = input.nextLine(); // 测试数字已经超过long型上限了,所以用字符串
int count = 0;
for (int i = 0; i < s.length(); i++) {
int temp = s.charAt(i) - '0';
count += temp;
}
String s1 = String.valueOf(count); // 结果
for (int i = 0; i < s1.length(); i++) {
System.out.print(trans(s1.charAt(i)));
System.out.print(i < s1.length() - 1 ? " " : ""); // 注意格式
}
}
public static String trans(char c) {
switch(c) {
case '0': return "ling";
case '1': return "yi";
case '2': return "er";
case '3': return "san";
case '4': return "si";
case '5': return "wu";
case '6': return "liu";
case '7': return "qi";
case '8': return "ba";
default: return "jiu";
}
}
}
1003 我要通过!
得到“答案正确”的条件是:
- 字符串中必须仅有 P、 A、 T这三种字符,不可以包含其它字符;
- 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
- 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a、 b、 c 均或者是空字符串,或者是仅由字母 A 组成的字符串。
这一题需要花点儿心思进行分析,重点在于三个条件并非独立,要把条件2、3联系起来看。比如,按照条件2来看AAPATAA是正确的,带入条件3不难得到AAPAATAAAA也是正确的,此时有a=AA,b=A,c=AA。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
boolean[] result = new boolean[n];
for (int i = 0; i < n; i++) {
String s = input.next(); // 紧接着nextInt()用nextLine()的话首行读不到
result[i] = isRight(s, result, i); // 判断正误
}
for (int i = 0; i < n; i++) {
System.out.println(result[i] ? "YES" : "NO");
}
}
private static boolean isRight(String s, boolean[] result, int i) {
int pcount = 0;
int acount = 0;
int tcount = 0;
for (int j = 0; j < s.length(); j++) {
char c = s.charAt(j); // 条件一:不可有除了P、A、T外的字符
switch (c) {
case 'P': pcount++; continue;
case 'A': acount++; continue;
case 'T': tcount++; continue;
default: return false;
}
}
if(pcount!=1 || tcount!=1 || acount==0) // 条件二:包含连续PAT的字符串,且其余字符(若存在)只可以是若干个A
return false;
/*这部分子串判断是条件二的直译,但是条件三所用的判断式可包含此情况,因而非必要
String sample = "PAT";
for (int j = 0; j < s.length() - 2; j++) {
String temp = s.substring(j, j + 3);
if (temp.equals(sample))
return true;
}*/
int positionP = s.indexOf('P');
int positionT = s.indexOf('T');
if( positionP * (positionT - positionP - 1) != s.length() - 1 - positionT ) // 条件三:要想在P、T中间增加A的个数,必须在左侧增加与右侧相同的内容
return false;
return true;
}
}
1004 成绩排名
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt(); // 学生个数
Student[] st = new Student[n];
int maxG = 0, minG = 100;
int minS = 0, maxS = 0;
for (int i = 0; i < n; i++) {
String name = input.next();
String studentID = input.next();
int grade = input.nextInt();
st[i] = new Student(name, studentID, grade);
if(grade > maxG) {
maxG = grade;
maxS = i;
}
if(grade < minG) {
minG = grade;
minS = i;
}
}
System.out.println(st[maxS].name+" "+st[maxS].studentID);
System.out.println(st[minS].name+" "+st[minS].studentID);
}
}
class Student {
String name;
String studentID;
int grade = 0;
public Student() {
}
public Student(String str1, String str2, int g) {
this.name = str1;
this.studentID = str2;
this.grade = g;
}
}
1005 继续(3n+1)猜想
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int k = input.nextInt(); // 输入的数字个数
ArrayList<Integer> digit = new ArrayList<>(); // 存放输入的数字,即待验证的,经过验证最后仅留下关键数字
ArrayList<Integer> result = new ArrayList<>(); //存放变换过程中的数字,即被覆盖的
for (int i = 0; i < k; i++) {
int temp = input.nextInt();
digit.add(temp);
while (temp != 1) {
if (temp % 2 == 0)
temp = temp / 2;
else
temp = (temp * 3 + 2) / 2;
result.add(temp);
}
}
for (int i = digit.size() - 1; i >= 0; i--) {
if (result.contains(digit.get(i)))
digit.remove(i); // 已被覆盖的移除
}
Collections.sort(digit); // 由小到大排列
for(int j = digit.size() - 1; j > 0 ; j--) {
// 由大到小输出
System.out.print(digit.get(j)+" ");
}
System.out.println(digit.get(0));
}
}
1006 换个格式输出整数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt(); // 小于1000的数字
int nCount = n