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

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

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


目录

1031、查验身份证(分数 15)

1032、挖掘机技术哪家强(分数 20)

1033、旧键盘打字(分数 20)

1034、有理数四则运算(分数 20)

1035、插入与归并(分数 25)


1031、查验身份证(分数 15)

题目描述:

代码实现:

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int[] w = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
        String M = "10X98765432";
        int N = in.nextInt();
        boolean flag = true;
        in.nextLine();
        for(int i = 0;i < N;i ++){
            String input = in.nextLine().trim();
            String id = input.substring(0,17);
            if(id.contains("X")){
                if(i == N - 1){
                    System.out.print(input);
                    flag = false;
                } else {
                System.out.println(input);
                flag = false;
                }
            }
            else{
                char m = input.charAt(17);
                int Z = 0;
                for(int j = 0;j < 17;j ++){
                    Z += (int) (id.charAt(j) - '0') * w[j];
                }
                char pattern = M.charAt(Z % 11);
                if(m == pattern){
                    continue;
                }else{
                    if(i == N - 1){
                    System.out.print(input);
                    flag = false;
                    } else {
                    System.out.println(input);
                    flag = false;
                    }
                }
            }
        }
        if(flag)        System.out.println("All passed");
    }
}

运行结果:


1032、挖掘机技术哪家强(分数 20)

题目描述:

代码实现:

#include <iostream>
using namespace std;

//试了Java,试了Python,都被相同测试点卡时卡得死死的,换C++吧
int main(){
    int N, id, score;
    //测试点3(坑点):resId初始值不能为-1
    int resId = 1, resScore = -1;
    int scores[100100] = {0};
    cin >> N;
    if(N == 0)      return 0;
    while(N){
        cin >> id >> score;
        scores[id] += score;
        if(scores[id] >= resScore){
            resId = id;
            resScore = scores[id];
        }
        N --;
    }
    cout << resId << " " << resScore << endl;
    return 0;
}

运行结果:


1033、旧键盘打字(分数 20)

题目描述:

代码实现:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
        String[] pattern = bfr.readLine().split("");
        String txt = bfr.readLine();
        bfr.close();
        boolean flag = false;
        for(String c : pattern){
            if (!flag){
                if (c.equals("+")) {
                    txt = txt.replaceAll("[A-Z]", "");
                    txt = txt.replace(c, "");
                    flag = true;
                }
            }
            if(c.matches("[A-Z]")){
                if (txt.contains(c.toLowerCase())){
                    String c_ = c.toLowerCase();
                    txt = txt.replace(c_,"");
                }
            }
            if (txt.contains(c)){
                txt = txt.replace(c,"");
            }
        }
        System.out.println(txt);
    }
}

运行结果:


1034、有理数四则运算(分数 20)

题目描述:

代码实现:

//Java又在时间上爆了,始终A不过,这里换C++参考了一手柳神的代码:https://blog.csdn.net/liuchuo/article/details/51994755
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;

ll a, b, c, d;
ll gcd(ll t1, ll t2) {
    return t2 == 0 ? t1 : gcd(t2, t1 % t2);
}
void func(ll m, ll n) {
    if (m * n == 0) {
        printf("%s", n == 0 ? "Inf" : "0");
        return;
    }
    bool flag = ((m < 0 && n > 0) || (m > 0 && n < 0));
    m = abs(m); n = abs(n);
    ll x = m / n;
    printf("%s", flag ? "(-" : "");
    if (x != 0) printf("%lld", x);
    if (m % n == 0) {
        if(flag) printf(")");
        return ;
    }
    if (x != 0) printf(" ");
    m = m - x * n;
    ll t = gcd(m, n);
    m = m / t; n = n / t;
    printf("%lld/%lld%s", m, n, flag ? ")" : "");
}
int main() {
    scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d);
    func(a, b); printf(" + "); func(c, d); printf(" = "); func(a * d + b * c, b * d); printf("\n");
    func(a, b); printf(" - "); func(c, d); printf(" = "); func(a * d - b * c, b * d); printf("\n");
    func(a, b); printf(" * "); func(c, d); printf(" = "); func(a * c, b * d); printf("\n");
    func(a, b); printf(" / "); func(c, d); printf(" = "); func(a * d, b * c);
    return 0;
}

运行结果:


1035、插入与归并(分数 25)

题目描述:

代码实现:

import java.io.*;
import java.util.Arrays;

public class Main {
    //插入排序的意思是,首先由数组[0]开始检查,以此与后面的位就行比较,如果[n+1]小于[n],则n + 1位的数字找到正确的位置
    //归并排序,是每两个数字进行比较,如果前面的数字大于后面的数字,则交换
    public static void sort(int[] arr, int from, int to) {
        for (int i = from; i < to; i++)
            for (int j = i + 1; j <= to; j++)
                if (arr[j] < arr[i]) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
    }
    
    public static void main(String[] args) throws IOException{
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(bf.readLine());
        String[] str1 = bf.readLine().trim().split("\\s+");
        String[] str2 = bf.readLine().trim().split("\\s+");
        int[] arr1 = new int[N];
        int[] arr2 = new int[N];
        for(int i=0;i<N;i++) {
            arr1[i] = Integer.parseInt(str1[i]);
            arr2[i] = Integer.parseInt(str2[i]);
        }
        boolean insert = true;	//首先先判断是插入还是归并
        int loc = 0;
        while(arr2[loc] <= arr2[loc + 1] && loc < N){
            loc++;				//记录出现排序未完成的数字
        }
        for (int j = loc + 1; j < N; j++) {
            if (arr1[j] != arr2[j]) {
                insert = false;	//检查后续数字是否一致,如果有变,则是归并,如果未变,则是插入;
                break;
            }
        }

        if(insert) {
            System.out.print("Insertion Sort\n");
            sort(arr1, 0, loc + 1);
        }else {
            System.out.print("Merge Sort\n");
            int cache = 1;
            boolean flag = false;
            while (flag == false) {
                flag = Arrays.equals(arr1, arr2);
                cache = cache * 2;
                int a = 0;
                for (a = 0; a < N / cache; a++) sort(arr1, a * cache, (a + 1) * cache - 1);
                sort(arr1, N / cache * cache, N - 1);
            }
        }
        for (int i = 0; i < N - 1; i++) System.out.print(arr1[i] + " ");
        System.out.print(arr1[N - 1]);
    }
}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值