✨欢迎您的订阅✨
PAT乙级专栏(更新完毕):
【Basic Level】
PAT甲级专栏(更新ing):
【Advanced Level】
目录
1001 A+B Format【考察:字符串处理】
1002 A+B for Polynomials【考察:模拟】
1003 Emergency【考察:Dijkstra算法】
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).
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.
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.
-1000000 9
-999,991
代码长度限制
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);
}
}
This time, you are supposed to find A+B where A and B are two polynomials.
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<⋯ 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. 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB 题目大意: 代码: 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. 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. 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. 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB 题目大意: 代码:Output Specification:
Sample Input:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output:
3 2 1.5 1 2.9 0 3.2
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
1003 Emergency【考察:Dijkstra算法】
Input Specification:
Output Specification:
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
Sample Output:
2 4
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]);
}
}