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

相关文章
|
6月前
|
数据可视化 大数据
数据统计分析 — 数据可视化
数据统计分析 — 数据可视化
87 0
|
3天前
|
人工智能 自然语言处理 Shell
深度评测 | 仅用3分钟,百炼调用满血版 Deepseek-r1 API,百万Token免费用,简直不要太爽。
仅用3分钟,百炼调用满血版Deepseek-r1 API,享受百万免费Token。阿里云提供零门槛、快速部署的解决方案,支持云控制台和Cloud Shell两种方式,操作简便。Deepseek-r1满血版在推理能力上表现出色,尤其擅长数学、代码和自然语言处理任务,使用过程中无卡顿,体验丝滑。结合Chatbox工具,用户可轻松掌控模型,提升工作效率。阿里云大模型服务平台百炼不仅速度快,还确保数据安全,值得信赖。
157353 24
深度评测 | 仅用3分钟,百炼调用满血版 Deepseek-r1 API,百万Token免费用,简直不要太爽。
|
5天前
|
人工智能 API 网络安全
用DeepSeek,就在阿里云!四种方式助您快速使用 DeepSeek-R1 满血版!更有内部实战指导!
DeepSeek自发布以来,凭借卓越的技术性能和开源策略迅速吸引了全球关注。DeepSeek-R1作为系列中的佼佼者,在多个基准测试中超越现有顶尖模型,展现了强大的推理能力。然而,由于其爆火及受到黑客攻击,官网使用受限,影响用户体验。为解决这一问题,阿里云提供了多种解决方案。
16965 37
|
13天前
|
机器学习/深度学习 人工智能 自然语言处理
PAI Model Gallery 支持云上一键部署 DeepSeek-V3、DeepSeek-R1 系列模型
DeepSeek 系列模型以其卓越性能在全球范围内备受瞩目,多次评测中表现优异,性能接近甚至超越国际顶尖闭源模型(如OpenAI的GPT-4、Claude-3.5-Sonnet等)。企业用户和开发者可使用 PAI 平台一键部署 DeepSeek 系列模型,实现 DeepSeek 系列模型与现有业务的高效融合。
|
5天前
|
并行计算 PyTorch 算法框架/工具
本地部署DeepSeek模型
要在本地部署DeepSeek模型,需准备Linux(推荐Ubuntu 20.04+)或兼容的Windows/macOS环境,配备NVIDIA GPU(建议RTX 3060+)。安装Python 3.8+、PyTorch/TensorFlow等依赖,并通过官方渠道下载模型文件。配置模型后,编写推理脚本进行测试,可选使用FastAPI服务化部署或Docker容器化。注意资源监控和许可协议。
1310 8
|
13天前
|
人工智能 搜索推荐 Docker
手把手教你使用 Ollama 和 LobeChat 快速本地部署 DeepSeek R1 模型,创建个性化 AI 助手
DeepSeek R1 + LobeChat + Ollama:快速本地部署模型,创建个性化 AI 助手
3416 117
手把手教你使用 Ollama 和 LobeChat 快速本地部署 DeepSeek R1 模型,创建个性化 AI 助手
|
8天前
|
人工智能 自然语言处理 API
DeepSeek全尺寸模型上线阿里云百炼!
阿里云百炼平台近日上线了DeepSeek-V3、DeepSeek-R1及其蒸馏版本等六款全尺寸AI模型,参数量达671B,提供高达100万免费tokens。这些模型在数学、代码、自然语言推理等任务上表现出色,支持灵活调用和经济高效的解决方案,助力开发者和企业加速创新与数字化转型。示例代码展示了如何通过API使用DeepSeek-R1模型进行推理,用户可轻松获取思考过程和最终答案。
|
5天前
|
人工智能 自然语言处理 程序员
如何在通义灵码里用上DeepSeek-V3 和 DeepSeek-R1 满血版671B模型?
除了 AI 程序员的重磅上线外,近期通义灵码能力再升级全新上线模型选择功能,目前已经支持 Qwen2.5、DeepSeek-V3 和 R1系列模型,用户可以在 VSCode 和 JetBrains 里搜索并下载最新通义灵码插件,在输入框里选择模型,即可轻松切换模型。
934 14
|
12天前
|
API 开发工具 Python
阿里云PAI部署DeepSeek及调用
本文介绍如何在阿里云PAI EAS上部署DeepSeek模型,涵盖7B模型的部署、SDK和API调用。7B模型只需一张A10显卡,部署时间约10分钟。文章详细展示了模型信息查看、在线调试及通过OpenAI SDK和Python Requests进行调用的步骤,并附有测试结果和参考文档链接。
1938 9
阿里云PAI部署DeepSeek及调用
|
9天前
|
人工智能 数据可视化 Linux
【保姆级教程】3步搞定DeepSeek本地部署
DeepSeek在2025年春节期间突然爆火出圈。在目前DeepSeek的网站中,极不稳定,总是服务器繁忙,这时候本地部署就可以有效规避问题。本文以最浅显易懂的方式带读者一起完成DeepSeek-r1大模型的本地部署。

热门文章

最新文章