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

相关文章
|
移动开发 C语言
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
98 0
PAT (Advanced Level) Practice - 1057 Stack(30 分)
PAT (Advanced Level) Practice - 1057 Stack(30 分)
110 0
PAT (Advanced Level) Practice - 1057 Stack(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
121 0
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
134 0
PAT (Advanced Level) Practice - 1022 Digital Library(30 分)
PAT (Advanced Level) Practice - 1022 Digital Library(30 分)
125 0
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
143 0
PAT (Advanced Level) Practice - 1129 Recommendation System(25 分)
PAT (Advanced Level) Practice - 1129 Recommendation System(25 分)
107 0
PAT (Advanced Level) Practice - 1139 First Contact(30 分)
PAT (Advanced Level) Practice - 1139 First Contact(30 分)
114 0
PAT (Advanced Level) Practice - 1148 Werewolf - Simple Version(20 分)
PAT (Advanced Level) Practice - 1148 Werewolf - Simple Version(20 分)
128 0
PAT (Advanced Level) Practice - 1049 Counting Ones(30 分)
PAT (Advanced Level) Practice - 1049 Counting Ones(30 分)
120 0
下一篇
DataWorks