PAT (Advanced Level) Practice:1~3题

简介: ​✨欢迎您的订阅✨PAT乙级专栏(更新完毕):👉🏻【Basic Level】👈🏻PAT甲级专栏(更新ing):👉🏻【Advanced Level】👈🏻​

 

欢迎您的订阅

PAT乙级专栏(更新完毕):

👉🏻【Basic Level】👈🏻

PAT甲级专栏(更新ing):

👉🏻【Advanced Level】👈🏻

目录

1001 A+B Format【考察:字符串处理】

1002 A+B for Polynomials【考察:模拟】

1003 Emergency【考察:Dijkstra算法】


1001 A+B Format【考察:字符串处理】

Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

Input Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106. The numbers are separated by a space.

Output Specification:

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input:

-1000000 9

image.gif

Sample Output:

-999,991

image.gif

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

题目大意:

计算A+B的和,然后以每三位加⼀个”.”的格式输出~

代码:

import java.io.*;
/**
 * @author yx
 */
public class Main {
    static PrintWriter out=new PrintWriter(System.out);
    static BufferedReader ins=new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer in=new StreamTokenizer(ins);
    public static void main(String[] args) throws IOException {
        in.nextToken();
        int A=(int) in.nval;
        in.nextToken();
        int B=(int) in.nval;
        char[] ans=Integer.toString(A+B).toCharArray();
        int flag;
        int length=ans.length;
        int k=1;
        String s="";
        if(ans[0]!='-') {
            if(length%3==0){
                flag=length/3-1;
            }else {
                flag=length/3;
            }
            for (int i = length-1; i >=0; i--) {
                s=ans[i]+s;
                if(k%3==0&&flag!=0){
                    s=","+s;
                    flag--;
                }
                k++;
            }
        }else {
            length=length-1;
            if(length%3==0){
                flag=length/3-1;
            }else {
                flag=length/3;
            }
            System.out.print("-");
            for (int i = length; i >=1 ; i--) {
                s=ans[i]+s;
                if(k%3==0&&flag!=0){
                    s=","+s;
                    flag--;
                }
                k++;
            }
        }
        System.out.println(s);
    }
}

image.gif

1002 A+B for Polynomials【考察:模拟】

This time, you are supposed to find A+B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N1 aN1 N2 aN2 ... NK aNK

where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤NK<⋯<N2<N1≤1000.

Output Specification:

For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

image.gif

Sample Output:

3 2 1.5 1 2.9 0 3.2

image.gif

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

题目大意:

计算多项式A+B的和~

代码:

package PAT_甲.PAT甲_1_10;
import java.io.*;
import java.util.Arrays;
/**
 * @author yx
 * @date 2022-10-01 17:02
 */
public class NO1002_数组满分 {
    static PrintWriter out =new PrintWriter(System.out);
    static BufferedReader ins=new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer in=new StreamTokenizer(ins);
    public static void main(String[] args) throws IOException {
        //注意用in.nval来读入数据的时候,一定要用in.nextToken
        in.nextToken();
        int n=(int) in.nval;
        double[] nums=new double[1005];
        Arrays.fill(nums,0.0);
        int ans=0;
        for(int i=0;i<n;i++){
            in.nextToken();
            int zs=(int) in.nval;
            in.nextToken();
            double xs=in.nval;
            nums[zs]+=xs;
        }
        in.nextToken();
        int m=(int) in.nval;
        for (int i = 0; i < m; i++) {
            in.nextToken();
            int zs=(int) in.nval;
            in.nextToken();
            double xs=in.nval;
            nums[zs]+=xs;
        }
        for (int i = 0; i < 1005; i++) {
            if(nums[i]!=0.0){
                ans++;
            }
        }
        System.out.print(ans);
        for (int i = 1004; i >=0 ; i--) {
            if(nums[i]!=0.0){
                //这个地方要注意用%.1f,不然第二个测试点会不通过
                System.out.printf(" %d %.1f",i,nums[i]);
            }
        }
    }
}

image.gif

1003 Emergency【考察:Dijkstra算法

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input Specification:

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤500) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input:

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

image.gif

Sample Output:

2 4

image.gif

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

题目大意:

n个城市m条路,每个城市有救援⼩组,所有的边的边权已知。给定起点和终点,求从起点到终点的最短路径条数以及最短路径上的救援⼩组数⽬之和。如果有多条就输出点权(城市救援⼩组数⽬)最⼤的那个~

代码:

import java.util.Arrays;
import java.util.Scanner;
public class Main{
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int city = input.nextInt();// 城市数
    int road = input.nextInt();// 道路数
    int current = input.nextInt();// 当前城市
    int destination = input.nextInt();// 目的地
    input.nextLine();
    String[] rescue = input.nextLine().split(" ");
    int[] rescue_queue = new int[rescue.length];// 救援队数组,保存救援队数目
    for (int i = 0; i < rescue.length; i++) {
      rescue_queue[i] = Integer.parseInt(rescue[i]);
    }
    int[] dis = new int[city];
    int[] w = new int[city];
    int[] num = new int[city];
    int[][] e = new int[city][city]; // 边集
    boolean[] vis = new boolean[city];
    final int inf = 99999999;
    Arrays.fill(vis, false);
    Arrays.fill(num, 0);
    Arrays.fill(dis, inf);
    for (int i = 0; i < city; i++) {
      Arrays.fill(e[i], inf);
    }
    for (int i = 0; i < road; i++) {
      int fi, sec, adj;
      fi = input.nextInt();
      sec = input.nextInt();
      adj = input.nextInt();
      e[fi][sec] = e[sec][fi] = adj;
    }
    dis[current] = 0;
    w[current] = rescue_queue[current];
    num[current] = 1;
    for (int i = 0; i < city; i++) {
      int u = -1, minn = inf;
      for (int j = 0; j < city; j++) {
        if (vis[j] == false && dis[j] < minn) {
          u = j;
          minn = dis[j];
        }
      }
      if (u == -1)
        break;
      vis[u] = true;
      for (int v = 0; v < city; v++) {
        if (vis[v] == false && e[u][v] != inf) {
          if (dis[u] + e[u][v] < dis[v]) {
            dis[v] = dis[u] + e[u][v];
            num[v] = num[u];
            w[v] = w[u] + rescue_queue[v];
          } else if (dis[u] + e[u][v] == dis[v]) {
            num[v] = num[v] + num[u];
            if (w[u] + rescue_queue[v] > w[v])
              w[v] = w[u] + rescue_queue[v];
          }
        }
      }
    }
    System.out.printf("%d %d",num[destination],w[destination]);
  }
}

image.gif

相关文章
|
设计模式 Java 应用服务中间件
Tomcat 架构原理解析到设计借鉴
Tomcat 架构原理解析到设计借鉴
421 0
|
XML Dubbo fastjson
FastJson - 设置默认参数,全局配置方式及爬坑
FastJson - 设置默认参数,全局配置方式及爬坑
2026 0
|
机器学习/深度学习 算法 安全
探索现代操作系统的内核设计与优化
在当今数字化时代,操作系统的内核是计算机系统稳定、高效运行的关键。本文深入探讨了现代操作系统内核的设计原则和优化方法,从微内核到宏内核,详细分析了它们各自的优缺点,并探讨了未来内核的发展趋势和创新方向。
257 28
|
6月前
|
算法
学会二分法,有这一篇就够啦!
本文由blue撰写于2024年9月,深入讲解二分法这一基础但不简单的算法。文章从二分法的两大经典应用场景——二分查找与二分答案出发,详细解析其原理与实现。通过实例代码(如LeetCode第704题)和竞赛题目,探讨了不同区间定义(左闭右闭、左闭右开)下的实现方式,并延伸到寻找目标值首次/最后出现位置及二分答案的实际应用。适合初学者系统掌握二分法的核心思想与技巧。
673 17
|
6月前
|
数据采集 算法 数据安全/隐私保护
【硬件测试】基于FPGA的MSK调制解调系统系统开发与硬件片内测试,包含信道模块,误码统计模块,可设置SNR
本文基于FPGA实现MSK调制解调系统,采用Verilog开发,包含同步模块、高斯信道模拟、误码率统计等功能。相比仿真版本,新增ILA数据采集与VIO在线SNR设置模块。通过硬件测试验证,展示不同SNR(如10dB和16dB)下的性能表现。研究聚焦软件无线电领域,优化算法复杂度以适应硬件限制,利用MSK恒定包络、相位连续等特性提升频谱效率。核心代码实现信号生成、调制解调、滤波及误码统计,提供完整的硬件设计与分析方案。
198 19
|
10月前
|
运维 安全 Devops
DevOps实践:持续集成与持续部署(CI/CD)的自动化之路
【10月更文挑战第22天】在软件交付的快速迭代中,DevOps文化和实践成为企业加速产品上市、保证质量和提升客户满意度的关键。本文将通过一个实际案例,深入探讨如何利用持续集成(Continuous Integration, CI)和持续部署(Continuous Deployment, CD)实现软件开发流程的高效自动化,包括工具选择、流程设计以及问题解决策略。我们将一起探索代码从编写到部署的全自动化旅程,揭示其对企业运维效率和产品质量所带来的深远影响。
|
Java 测试技术 Maven
单元测试问题之在Maven项目中引入JUnit 5和Mockito的依赖如何解决
单元测试问题之在Maven项目中引入JUnit 5和Mockito的依赖如何解决
792 1
|
安全 Java 数据安全/隐私保护
SpringSecurity 认证流程
通过了解SpringSecurity核心组件后,就可以进一步了解其认证的实现流程了。
210 0
|
JSON 算法 数据安全/隐私保护
聊聊 JSON Web Token (JWT) 和 jwcrypto 的使用
本文介绍了 JSON Web Token (JWT) 和 Python 中的 `jwcrypto` 库。JWT 是一种用于安全传输信息的紧凑型令牌,常用于身份验证。它由 Header、Payload 和 Signature 三部分组成,具有紧凑性、自包含和安全性等特点。`jwcrypto` 库提供了 JWT 的生成、验证、加密、解密及签名功能。通过该库,可以使用 RSA 等算法创建和验证 JWT,同时管理密钥对。安装 `jwcrypto` 可用 `pip install jwcrypto`,并示例展示了如何生成签名 JWT 和密钥对。
聊聊 JSON Web Token (JWT) 和 jwcrypto 的使用
|
存储 关系型数据库 MySQL
XtraBackup的工作原理是什么?
【5月更文挑战第13天】XtraBackup的工作原理是什么?
690 0