浙大PAT 乙级(题号1001~1005)自解全AC Java实现

其他题解收录都在这了👇👇👇

PAT 乙级(Basic Level) Practice 题解合集(全AC版)


 

目录

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

1002、写出这个数 (分数 20)

1003、我要通过!(分数 20)

1004、成绩排名(分数 20)

1005、继续(3n+1)猜想(分数 25)


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

题目描述:

代码实现:

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int count = 0;
        while(true){
            if(n == 1)  break;
            if(n % 2 == 0){
                n = n / 2;
                count ++;
            }
            else{
                n = (3 * n + 1) / 2;
                count ++;
            }
        }
        System.out.println(count);
    }
}

结果:

 


1002、写出这个数 (分数 20)

题目描述:

代码实现:

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        String n = in.nextLine();
        HashMap<String,String> NumDict = new HashMap<>();
        StringBuffer res = new StringBuffer();
        NumDict.put("0","ling");
        NumDict.put("1","yi");
        NumDict.put("2","er");
        NumDict.put("3","san");
        NumDict.put("4","si");
        NumDict.put("5","wu");
        NumDict.put("6","liu");
        NumDict.put("7","qi");
        NumDict.put("8","ba");
        NumDict.put("9","jiu");
        int sum = 0;
        for(int i = 0;i < n.length();i ++){
            sum += n.charAt(i) - '0';
        }
        String sum_ = String.valueOf(sum);
        for(int i = 0;i < sum_.length();i ++){
            res.append(NumDict.get(sum_.substring(i,i+1)) + " ");
        }
        System.out.println(res.substring(0,res.length() - 1));
    }
}

结果:


1003、我要通过!(分数 20)

题目描述:

代码实现:

import java.util.*;

public class Main{
    public static boolean onlyPAT(char[] s){
        for(char c : s){
            if(c != 'P' && c != 'A' && c != 'T')
                return false;
        }
        return true;
    }
    
    public static void main(String[] args){
        //不能定义一个Scanner对象即输入整数又输入字符串类型的,
        //我们会尝试生成多个Scanner对象,分别输入int类型和String类型
        Scanner in = new Scanner(System.in);
        int n = Integer.parseInt(in.nextLine());
        for(int i = 0;i < n;i ++){
            String txt = in.nextLine();
            if(onlyPAT(txt.toCharArray())){     //筛条件一
                int A = 0, P = 0, T = 0;     //'P'、'A'、'T'字符的个数
                int locP = -1, locT = -1;
                for(int j = 0;j < txt.length();j ++){
                    switch(txt.charAt(j)){
                        case 'A':
                            A ++;
                            break;
                        case 'P':
                            P ++;
                            locP = j;
                            break;
                        case 'T':
                            T ++;
                            locT = j;
                            break;
                    }
                }//P左侧、P~T、T右侧三区间'A'的个数分别为n1、n2、n3, 规律为n1 * n2 = n3
                if(A != 0 && P == 1 && T == 1 && locT - locP > 1 && locP * (locT - locP - 1) == txt.length() - locT - 1){
                    if(i == n - 1)
                        System.out.print("YES");
                    else    System.out.println("YES");
                }
                else{
                    if(i == n - 1)
                        System.out.print("NO");
                    else    System.out.println("NO");
                }
            }else{
                if(i == n - 1)
                        System.out.print("NO");
                else    System.out.println("NO");
            }
        }
    }
}

结果:


1004、成绩排名(分数 20)

题目描述:

代码实现:

import java.util.*;
import java.lang.*;
class Student implements Comparable<Student>{
    String name;
    String id;
    int score;
    Student(String name, String id,int score){
        this.name = name;
        this.id = id;
        this.score = score;
    }
    public int compareTo(Student other){
        if(this.score > other.score){
            return -1;
        }else if(this.score == other.score){
            return 0;
        }else{
            return 1;
        }
    }
    public String toString(){
        return this.name + " " + this.id;
    }
}



public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String space = in.nextLine();
        List<Student> stus = new ArrayList<>();
        while(n > 0){
            String input = in.nextLine();  
		    String[] info = input.split(" ");
            String name = info[0];
            String id = info[1];
            int score = Integer.parseInt(info[2]);
            stus.add(new Student(name, id, score));
            n--;
        }
        Collections.sort(stus);
        System.out.println(stus.get(0));
        System.out.println(stus.get(stus.size() - 1));
    }
}

结果:


1005、继续(3n+1)猜想(分数 25)

题目描述:

代码实现:

import java.util.*;

public class Main{
    static List<Integer> keyNum = new ArrayList<>();        //存关键数
    static List<Integer> coverNum = new ArrayList<>();      //存覆盖数
    //注意点*:所有的覆盖数被计算出来后,才能从原始数据中通过判断有没有包含在覆盖数列表里,从而得出是不是关键数
    public static void Callatz(int n){
        if(n == 1)
            return;
        if(n % 2 == 0){
            n = n / 2;
            if(coverNum.contains(n))        //被覆盖即已被计算过,无需再往下
                return;
            else{
                coverNum.add(n);
                Callatz(n);
            }
        }
        else{
            n = (3 * n + 1) / 2;
            if(coverNum.contains(n))
                return;
            else{
                coverNum.add(n);
                Callatz(n);
            }
        }
    }
    
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int K = Integer.parseInt(in.nextLine());
        String[] nums = in.nextLine().split(" ");
        int n = Integer.parseInt(nums[0]);
        Callatz(n);
        for(int i = 1;i < nums.length;i ++){
            n = Integer.parseInt(nums[i]);
            Callatz(n);
        }
        for(String s : nums){
            int N = Integer.parseInt(s);
            if(!coverNum.contains(N))
                keyNum.add(N);
        }
        Collections.sort(keyNum, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                if(o1 > o2)
                    return -1;
                else if(o1 < o2)
                    return 1;
                else return 0;
            }
        });
        for(int i = 0;i < keyNum.size();i ++){
            if(i < keyNum.size() - 1)
                System.out.print(keyNum.get(i) + " ");
            else  System.out.println(keyNum.get(i));
        }
    }
}

结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值