【PAT】乙级JAVA实现,1001-1005题

1001 害死人不偿命的(3n+1)猜想 

卡拉兹(Callatz)猜想:

对任何一个正整数 n,如果它是偶数,那么把它砍掉一半;如果它是奇数,那么把 (3n+1) 砍掉一半。这样一直反复砍下去,最后一定在某一步得到 n=1。卡拉兹在 1950 年的世界数学家大会上公布了这个猜想,传说当时耶鲁大学师生齐动员,拼命想证明这个貌似很傻很天真的命题,结果闹得学生们无心学业,一心只证 (3n+1),以至于有人说这是一个阴谋,卡拉兹是在蓄意延缓美国数学界教学与科研的进展……

我们今天的题目不是证明卡拉兹猜想,而是对给定的任一不超过 1000 的正整数 n,简单地数一下,需要多少步(砍几下)才能得到 n=1

输入格式:

每个测试输入包含 1 个测试用例,即给出正整数 n 的值。

输出格式:

输出从 n 计算到 1 需要的步数。

输入样例:

3

输出样例:

5

 

import java.util.Scanner;
public class Main {
    public static void main(String arg[]){
        Scanner scanner = new Scanner(System.in);
        int number = scanner.nextInt();
        scanner.close();
        int count = 0;
        if (number >0 && number <=1000){
            while (number != 1){
                if (number%2 == 0){
                    number = number/2;
                }else {
                    number = (3*number +1)/2;
                }
                count++;
            }
            System.out.print(count);
        }
    }
}

 

1002 写出这个数

读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字。

输入格式:

每个测试输入包含 1 个测试用例,即给出自然数 n 的值。这里保证 n 小于 10100

输出格式:

在一行内输出 n 的各位数字之和的每一位,拼音数字间有 1 空格,但一行中最后一个拼音数字后没有空格。

输入样例:

1234567890987654321123456789

输出样例:

yi san wu
import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
    public static void main(String arg[]){
        String number[] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
        Scanner scanner = new Scanner(System.in);
        BigDecimal n = scanner.nextBigDecimal();
        scanner.close();
        String s = String.valueOf(n);
        char[] nChar = s.toCharArray();
        int[] nList = new int[nChar.length];
        int sum = 0;
        for (int i = 0; i < nChar.length; i++) {
            nList[i] = Integer.parseInt(nChar[i]+"");
            sum = sum + nList[i];
        }
        char[] sumChar=(String.valueOf(sum)).toCharArray();
        for(int j=0; j<sumChar.length; j++){
            for(int k=0;k < number.length; k++){
                if(Integer.parseInt(sumChar[j]+"") == k){
                    if(j < sumChar.length-1) {
                        System.out.print(number[k] + " ");
                    }if(j == sumChar.length-1){
                        System.out.print(number[k]);
                    }
                }
            }
        }
    }
}

 

1003 我要通过!

答案正确是自动判题系统给出的最令人欢喜的回复。本题属于 PAT 答案正确大派送 —— 只要读入的字符串满足下列条件,系统就输出答案正确,否则输出答案错误

得到答案正确的条件是:

  1. 字符串中必须仅有 P、 A、 T这三种字符,不可以包含其它字符;
  2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是空字符串,或者是仅由字母 A 组成的字符串;
  3. 如果 aPbTc 是正确的,那么 aPbATca 也是正确的,其中 a、 b、 c 均或者是空字符串,或者是仅由字母 A 组成的字符串。

现在就请你为 PAT 写一个自动裁判程序,判定哪些字符串是可以获得答案正确的。

输入格式:

每个测试输入包含 1 个测试用例。第 1 行给出一个正整数 n (<10),是需要检测的字符串个数。接下来每个字符串占一行,字符串长度不超过 100,且不包含空格。

输出格式:

每个字符串的检测结果占一行,如果该字符串可以获得答案正确,则输出 YES,否则输出 NO

输入样例:

8
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA
 

输出样例:

YES
YES
YES
YES
NO
NO
NO
NO
import java.util.Scanner;

public class Main {
    public static void main(String arg[]){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        in.nextLine();

        for(int i = 0; i < n; i++){
            String str = in.next();
            if(onlyPAT(str)){
                if(aPbTc(str)){
                    System.out.println("YES");
                }else {
                    System.out.println("NO");
                }
            }else {
                System.out.println("NO");
            }
        }
        in.close();
    }

    public static boolean onlyPAT(String str){
        for(int i = 0; i < str.length(); i++){
            if (str.charAt(i) != 'P' && str.charAt(i) != 'A' && str.charAt(i) != 'T'){
                return false;
            }
        }
        return true;
    }

    public static boolean aPbTc(String str){
        int a = str.indexOf("P");
        int c = str.length() - str.indexOf("T") - 1;
        int b = str.indexOf("T") - str.indexOf("P") - 1;
        if((b == 0 ) || (a * b != c)){
            return false;
        }else {
            return true;
        }
    }
}

 

 

1004 成绩排名

读入 n>0)名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。

输入格式:

每个测试输入包含 1 个测试用例,格式为

第 1 行:正整数 n
第 2 行:第 1 个学生的姓名 学号 成绩
第 3 行:第 2 个学生的姓名 学号 成绩
  ... ... ...
第 n+1 行:第 n 个学生的姓名 学号 成绩

 

其中姓名学号均为不超过 10 个字符的字符串,成绩为 0 100 之间的一个整数,这里保证在一组测试用例中没有两个学生的成绩是相同的。

输出格式:

对每个测试用例输出 2 行,第 1 行是成绩最高学生的姓名和学号,第 2 行是成绩最低学生的姓名和学号,字符串间有 1 空格。

输入样例:

3
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95

输出样例:

Mike CS991301
Joe Math990112

 

import java.util.*;

public class Main {
    public static void main(String arg[]){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        in.nextLine();

        ArrayList<Student> students = new ArrayList<Student>();

        for (int i = 0; i < n; i++) {
            String[] lineSplit = in.nextLine().split("\\s+");
            students.add(new Student(lineSplit[0], lineSplit[1], Integer.parseInt(lineSplit[2])));
        }

        Collections.sort(students);
        System.out.println(students.get(n-1));
        System.out.println(students.get(0));

    }

    static class Student implements Comparable<Student>{
        String name;
        String stuId;
        int score;

        public Student(String name, String stuId, int score){
            this.name = name;
            this.stuId = stuId;
            this.score = score;
        }

        public int compareTo(Student other){
            return (this.score - other.score);
        }

        public String toString(){
            return (this.name + " " + this.stuId);
        }
    }
}

 

 

1005 继续(3n+1)猜想

卡拉兹(Callatz)猜想已经在1001中给出了描述。在这个题目里,情况稍微有些复杂。

当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数。例如对 n=3 进行验证的时候,我们需要计算 358421,则当我们对 n=5842 进行验证的时候,就可以直接判定卡拉兹猜想的真伪,而不需要重复计算,因为这 4 个数已经在验证3的时候遇到过了,我们称 5842 是被 3“覆盖的数。我们称一个数列中的某个数 n 关键数,如果 n 不能被数列中的其他数字所覆盖。

现在给定一系列待验证的数字,我们只需要验证其中的几个关键数,就可以不必再重复验证余下的数字。你的任务就是找出这些关键数字,并按从大到小的顺序输出它们。

输入格式:

每个测试输入包含 1 个测试用例,第 1 行给出一个正整数 K (<100),第 2 行给出 K 个互不相同的待验证的正整数 n (1<n100)的值,数字间用空格隔开。

输出格式:

每个测试用例的输出占一行,按从大到小的顺序输出关键数字。数字间用 1 个空格隔开,但一行中最后一个数字后没有空格。

输入样例:

6
3 5 6 7 8 11

输出样例:

7 6
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int K = scanner.nextInt();
        long[] numList = new long[K];
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 0; i < K; i++) {
            numList[i] = scanner.nextLong();
            int a = Integer.parseInt(String.valueOf(numList[i]));
            arrayList.add(a);
        }
        for (int i = 0; i < arrayList.size(); i++) {
            K = arrayList.size();
            int a = arrayList.get(i);
            threeN(a, arrayList);
            if(K > arrayList.size()){
                for (int j = 0; j < arrayList.size(); j++) {
                    threeN(arrayList.get(j), arrayList);
                }
            }
        }
        Collections.sort(arrayList);
        for (int i = arrayList.size()-1; i >= 0; i--) {
            if (i == arrayList.size()-1){
                System.out.print(arrayList.get(i));
            }else {
                System.out.print(" " +arrayList.get(i));
            }
        }
    }

    public static void threeN(int i, ArrayList<Integer> arrayList){
        while (i != 1){
            if (i%2 == 0){
                i = i/2;
            } else {
                i = (3*i + 1)/2;
            }
            for (int j = 0; j < arrayList.size(); j++) {
                if(arrayList.get(j) == i){
                    arrayList.remove(j);
                }
            }
        }
    }
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值