浙大PAT 乙级(题号1011~1015)自解全AC Java | Python | C++实现

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

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


 

目录

1011、A+B 和 C(分数 15)

1012、数字分类(分数 20)

1013、数素数(分数 20)

1014、福尔摩斯的约会(分数 20)

1015、德才论(分数 25)


1011、A+B 和 C(分数 15)

题目描述:

代码实现:

n = eval(input())
for i in range(n):
    a,b,c = map(int, input().split())
    if a + b > c:
        print("Case #{}: true".format(i + 1))
    else:
        print("Case #{}: false".format(i + 1))

结果:


1012、数字分类(分数 20)

题目描述:

代码实现:

import java.util.*;
public class Main{
    private static List<Integer> A1 = new ArrayList<>();
    private static List<Integer> A2 = new ArrayList<>();
    private static List<Integer> A3 = new ArrayList<>();
    private static List<Integer> A4 = new ArrayList<>();
    private static List<Integer> A5 = new ArrayList<>();
    
    public static void Classified(int n){
        int res = n % 5;
        if(res == 0){
            if(n % 2 == 0)      A1.add(n);
        }
        else if(res == 1){
            A2.add(n);
        }
        else if(res == 2){
            A3.add(n);
        }
        else if(res == 3){
            A4.add(n);
        }
        else if(res == 4){
            A5.add(n);
        }
    }
    
    public static int A1Solution(List<Integer> A){
        int sum = 0;
        for(int a : A){
            sum += a;
        }
        return sum;
    }
    
    public static int A2Solution(List<Integer> A){
        int sum = 0;
        int p = 1;
        for(int a : A){
            sum += p * a;
            p *= -1;
        }
        return sum;
    }
    
    public static int A3Solution(List<Integer> A){
        return A.size();
    }
    
    public static double A4Solution(List<Integer> A){
        double sum = 0;
        for(int a : A){
            sum += a;
        }
        return sum / A.size();
    }
    
    public static int A5Solution(List<Integer> A){
        int max = A.get(0);
        for(int a : A){
            max = Math.max(max, a);
        }
        return max;
    }
    
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        while(N != 0){
            Classified(in.nextInt());
            N--;
        }
        if(A1.isEmpty())      System.out.printf("N ");
        else            System.out.printf("%d ", A1Solution(A1));
        if(A2.isEmpty())      System.out.printf("N ");
        else            System.out.printf("%d ", A2Solution(A2));
        if(A3.isEmpty())      System.out.printf("N ");
        else            System.out.printf("%d ", A3Solution(A3));
        if(A4.isEmpty())      System.out.printf("N ");
        else            System.out.printf("%.1f ", A4Solution(A4));
        if(A5.isEmpty())      System.out.printf("N");
        else            System.out.printf("%d", A5Solution(A5));
    }
}

结果:


1013、数素数(分数 20)

题目描述:

代码实现:

import java.util.*;

public class Main{
    public static boolean isPrime(int n){
        if(n == 2 || n == 3)      return true;
        for(int i = 2;i <= (int)Math.sqrt(n);i ++)
            if(n % i == 0)      return false;
        return true;
    }
    
    public static void main(String[] args){
        StringBuffer res = new StringBuffer();
        Scanner in = new Scanner(System.in);
        int low = in.nextInt();
        int high = in.nextInt();
        List<Integer> primes = new ArrayList<>();
        int count = 0;
        for(int n = 2;count < high;n++){
            if(isPrime(n)){
                count ++;            //1 2 3 4 5
                if(count >= low){    //2 3 5 7 11
                    primes.add(n);
                }
            }
        }
        int c = 1;
        for(int p : primes){
            if(c % 10 == 0)     res.append(p + "\n");
            else            res.append(p + " ");
            c ++;
        }
        System.out.println(res.substring(0,res.length() - 1));
    }
}

结果:


1014、福尔摩斯的约会(分数 20)

题目描述:

代码实现:

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        String str1 = in.nextLine();
        String str2 = in.nextLine();
        String str3 = in.nextLine();
        String str4 = in.nextLine();
        String[] days = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
        boolean first = false, second = false;      //first找星期几, second找小时的标记
        int h;
        for (int i = 0;i < str1.length() && i < str2.length();i ++){
            char chr1 = str1.charAt(i), chr2 = str2.charAt(i);
            if (chr1 == chr2){
                if ((chr1 >= 'A' && chr1 <= 'G') && !first){        //第一个大写字符A-G找星期几
                    int day = (chr1 - 'A');
                    System.out.print(days[day] + " ");
                    first = true;           //first找到
                    second = true;      //second可以开始找
                    continue;
                }
                if ((chr1 >= 'A' && chr1 <= 'N' || chr1 >= '0' && chr1 <= '9') && second){
                    //第二个满足条件找小时数h
                    if (chr1 >= 'A')        h = (chr1 - 'A') + 10;
                    else        h = (chr1 - '0');
                    System.out.printf("%02d", h);
                    second = false;
                }
            }

        }
        for (int i = 0;i < str3.length() && i < str4.length();i ++){
            char chr3 = str3.charAt(i), chr4 = str4.charAt(i);
            if (chr3 == chr4 && (chr3 >= 'a' && chr3 <= 'z' || chr3 >= 'A' && chr3 <= 'Z')){
                System.out.printf(":%02d", i);
                break;
            }
        }
    }
}

结果:


1015、德才论(分数 25)

题目描述:

代码实现:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

typedef struct Student{
    int ID;
    int De_Score;
    int Cai_Score;
}Stu;

bool compare(Student a, Student b){
    int sumA = a.De_Score + a.Cai_Score;
    int sumB = b.De_Score + b.Cai_Score;
    if(sumA == sumB){
        if(a.De_Score == b.De_Score){
            return a.ID < b.ID;
        }
        else return a.De_Score > b.De_Score;
    }
    else       return sumA > sumB;
}

int main(){
    vector<Student> rank1;
    vector<Student> rank2;
    vector<Student> rank3;
    vector<Student> rank4;
    int N, L, H, M = 0;
    scanf("%d %d %d", &N, &L, &H);
    int id, de, cai;
    while(N --){
        getchar();
        scanf("%d %d %d", &id, &de, &cai);
        if(de >= L && cai >= L){
                Stu student;
                student.ID = id;
                student.De_Score = de;
                student.Cai_Score = cai;
                M ++;
                if(de >= H && cai >= H)
                    rank1.push_back(student);
                else if(cai < H && de >= H)
                    rank2.push_back(student);
                else if(de < H && cai < H && de >= cai)
                    rank3.push_back(student);
                else      rank4.push_back(student);
        }
    }
    sort(rank1.begin(), rank1.end(), compare);
    sort(rank2.begin(), rank2.end(), compare);
    sort(rank3.begin(), rank3.end(), compare);
    sort(rank4.begin(), rank4.end(), compare);
    cout << M << endl;
    for(int i = 0;i < rank1.size();i ++){
        cout << rank1[i].ID << " " << rank1[i].De_Score << " " << rank1[i].Cai_Score << endl;
    }
    for(int i = 0;i < rank2.size();i ++){
        cout << rank2[i].ID << " " << rank2[i].De_Score << " " << rank2[i].Cai_Score << endl;
    }
    for(int i = 0;i < rank3.size();i ++){
        cout << rank3[i].ID << " " << rank3[i].De_Score << " " << rank3[i].Cai_Score << endl;
    }
    for(int i = 0;i < rank4.size();i ++){
        cout << rank4[i].ID << " " << rank4[i].De_Score << " " << rank4[i].Cai_Score;
        if(i < rank4.size() - 1)
            printf("\n");
    }
    return 0;
}

结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值