PAT (Advanced Level) Practice 1001-1005
PAT (Advanced Level) Practice 1001-1005
PAT 计算机程序设计能力考试 甲级 练习题
题库:PTA拼题A官网
背景
这是浙大背景的一个计算机考试
刷刷题练练手
在博客更新题解 每五题更新一次 共155题
题目目录
1001 A+B Format (20分)
1002 A+B for Polynomials (25分)
1003 Emergency (25分)
1004 Counting Leaves (30分)
1005 Spell It Right (20分)
总结
1001 签到题(格式)
1002 签到题(格式)
1003 多条最短路(Djikstra) 路径最大点权和
1004 DFS 统计树每层叶子节点个数
1005 签到题(格式)
题目详解
1001 A+B Format (20分)
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 −10^6 ≤ a, b ≤ 10^6. 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
Sample Output:
-999,991
这种题还用写么
需要注意的地方就是输出格式了
不过这种标准格式可以直接使用Java自带的Format库
AC代码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.NumberFormat;
import java.util.Scanner;
public class PAT1001 {
public static void main(String[] args) {
Scanner scanner = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int a = scanner.nextInt();
int b = scanner.nextInt();
int sum = a + b;
NumberFormat format = NumberFormat.getInstance();
String result = format.format(sum);
System.out.println(result);
}
}
1002 A+B for Polynomials (25分)
This time, you are supposed to find A+B where A and B are two polynomials.
Input
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
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
Sample Output
3 2 1.5 1 2.9 0 3.2
这题被坑了一会
因为答案输出要求始终精确到1位小数
即像1
或者0
都要输出成1.0
和0.0
这样
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.Scanner;
public class PAT1002 {
public static void main(String[] args) {
Scanner scanner = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
double[] pol = new double[1001];
for (int i = 0; i < 2; i++) {
int k = scanner.nextInt();
for (int j = 0; j < k; j++) {
int n = scanner.nextInt();
double a = scanner.nextDouble();
pol[n] += a;
}
}
DecimalFormat format = new DecimalFormat("0.0");
int count = 0;
StringBuilder result = new StringBuilder();
for (int i = pol.length - 1; i >= 0; i--) {
if (pol[i] == 0) {
continue;
}
count++;
result.append(" ").append(i).append(" ").append(format.format(pol[i]));
}
result.insert(0, count);
System.out.println(result.toString());
}
}
1003 Emergency (25分)
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
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
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
Sample Output
2 4
总算来了一道不是签到题的题目
很容易看出来这是一个节点和边都带权的无向图
使用狄杰斯特拉(Djikstra)
来求解最短路
话说每次用到这个算法就想到我同学用的狄杰特斯拉[手动狗头]
需要分别保存
road_count[i] 到达节点i当前共有多少条相同边权的路
people[i] 到达节点i后可聚集的最大救援队人数
dis[i] 到达节点i的最短路长度
pre[i] 访问节点i的前置节点(从哪个节点到的节点i)
vis[i] 节点i是否被访问过
这题使用邻接矩阵存储了图结构
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
public class PAT1003 {
public static void main(String[] args) {
Scanner scanner = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int N = scanner.nextInt();
int M = scanner.nextInt();
int start = scanner.nextInt();
int end = scanner.nextInt();
int[] vertex_team = new int[N];
for (int i = 0; i < N; i++) {
vertex_team[i] = scanner.nextInt();
}
int[][] map = new int[N][N];
for (int i = 0; i < N; i++) {
Arrays.fill(map[i], Integer.MAX_VALUE);
}
for (int i = 0; i < M; i++) {
int from = scanner.nextInt();
int to = scanner.nextInt();
int dis = scanner.nextInt();
map[from][to] = map[to][from] = dis;
}
int[] road_count = new int[N];
road_count[start] = 1;
int[] people = new int[N];
people[start] = vertex_team[start];
int[] dis = new int[N];
Arrays.fill(dis, Integer.MAX_VALUE);
dis[start] = 0;
int[] pre = new int[N];
Arrays.fill(pre, -1);
pre[start] = start;
boolean[] vis = new boolean[N];
for (int i = 0; i < N; i++) {
int now = -1, min = Integer.MAX_VALUE;
for (int j = 0; j < N; j++) {
if (!vis[j] && dis[j] < min) {
now = j;
min = dis[j];
}
}
if (now == -1) {
break;
}
vis[now] = true;
for (int j = 0; j < N; j++) {
if (!vis[j] && map[now][j] != Integer.MAX_VALUE) {
if (dis[now] + map[now][j] < dis[j]) {
dis[j] = dis[now] + map[now][j];
pre[j] = pre[now];
people[j] = people[now] + vertex_team[j];
road_count[j] = road_count[now];
} else if (dis[now] + map[now][j] == dis[j]) {
road_count[j] += road_count[now];
if (people[now] + vertex_team[j] > people[j]) {
people[j] = people[now] + vertex_team[j];
}
}
}
}
}
System.out.printf("%d %d", road_count[end], people[end]);
}
}
1004 Counting Leaves (30分)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
Input
Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), the number of non-leaf nodes. Then M lines follow, each in the format:
ID K ID[1] ID[2] … ID[K]
where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID’s of its children. For the sake of simplicity, let us fix the root ID to be 01.
Output
For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.
The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output “0 1” in a line.
Sample Input
2 1
01 1 02
Sample Output
0 1
这题是树的遍历
看题意很容易想到层次遍历,却发现根据输入不太好构建这棵树
一开始是想这样做的:边读取边建立这一棵二叉树,然后层序遍历。发现题目里没有给明节点给出的顺序,也就无法合理的建树,建森林再连接的话只会增加此题难度。
于是改变思路:深搜一下这棵树
利用ArrayList[]保存所有输入结果,一开始还被题目的two-digit number给误导了,其实直接照int读入就行,读取全部输入之后对应的ArrayList[i]就代表了一个节点,如果其中没有孩子,就记录为一个叶子节点,对答案做一次更新即可。
其实题目给的count的取值只会是1和2,因为非叶子节点至多两个孩子,还可以据此优化。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
public class PAT1004 {
static ArrayList<Integer>[] tree = new ArrayList[128];
static int[] ans = new int[128];
static int max_depth = -1;
static void dfs(int index, int depth) {
if (tree[index].size() == 0) {
ans[depth]++;
max_depth = Math.max(max_depth, depth);
}
for (int child : tree[index]) {
dfs(child, depth + 1);
}
}
public static void main(String[] args) {
for (int i = 0; i < tree.length; i++) {
tree[i] = new ArrayList<>();
}
Scanner scanner = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int N = scanner.nextInt();
if (N == 0) {
System.out.println(N);
System.exit(0);
}
int M = scanner.nextInt();
for (int i = 0; i < M; i++) {
int root = scanner.nextInt();
int count = scanner.nextInt();
for (int j = 0; j < count; j++) {
int child = scanner.nextInt();
tree[root].add(child);
}
}
dfs(1, 0);
System.out.print(ans[0]);
for (int i = 1; i <= max_depth; i++) {
System.out.print(" " + ans[i]);
}
}
}
1005 Spell It Right (20分)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.
Input Specification:
Each input file contains one test case. Each case occupies one line which contains an N (<= 10^100).
Output Specification:
For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.
Sample Input:
12345
Sample Output:
one five
肉眼可见的签到题,一开始看到10的100次方,手打出BigInteger之后看了看题又删掉了。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class PAT1005 {
public static void main(String[] args) {
Scanner scanner = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
String input = scanner.nextLine();
int sum = 0;
int len = input.length();
for (int i = 0; i < len; i++) {
sum += input.charAt(i) - '0';
}
String[] words = {"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};
String output = Integer.toString(sum);
len = output.length();
System.out.print(words[output.charAt(0) - '0']);
for (int i = 1; i < len; i++) {
System.out.print(" " + words[output.charAt(i) - '0']);
}
}
}
后记
每天刷题频率不固定 故此系列博客也有咕咕咕的可能
PAT (Advanced Level) Practice 1001-1005的更多相关文章
- PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642
PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642 题目描述: Calculate a+b and output the sum i ...
- PAT (Advanced Level) Practice 1001 A+B Format (20 分)
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 Calculate a+b and ...
- PAT (Advanced Level) Practice 1001 A+B Format 分数 20
Calculate a+b and output the sum in standard format -- that is, the digits must be separated into gr ...
- PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642
PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642 题目描述: Given a non-negative integer N ...
- PAT (Advanced Level) Practice(更新中)
Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...
- PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642
PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...
- PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642
PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...
随机推荐
- nginx部署VUE跨域访问api
H5端配置跨域 nginx跨域配置 server { listen 80; charset utf-8; server_name you_dome_name;#location /tasklist.j ...
- 牛客练习赛$48E$ 小$w$的矩阵前$k$大元素 堆
正解:堆 解题报告: 传送门$QwQ$ 考虑把$b$从大往小排序,然后把$a_1+b_1,a_2+b_1,...,a_n+b_1$丢到堆里,顺便记录下$b$的下标 然后每次拿出一个最大值,设为$mx= ...
- $Noip2016/Luogu2822$ 组合数问题
$Luogu$ 看这题题解的时候看到一个好可爱的表情(●'◡'●)ノ♥ $Sol$ 首先注意到这题的模数是$k$.然而$k$并不一定是质数,所以不能用$C_n^m=\frac{n!}{m!(n-m)! ...
- php strcmp函数漏洞
strcmp函数漏洞 适用5.3版本以前的php 函数作用:字符串比较 要求传入字符串.如果传入非字符串呢? 结果函数报错!但是函数返回“0” . 虽然报错了但函数的判断却是“相等” 如何传入非字符 ...
- 1086 就不告诉你 (15分)C语言
做作业的时候,邻座的小盆友问你:"五乘以七等于多少?"你应该不失礼貌地微笑着告诉他:"五十三."本题就要求你,对任何一对给定的正整数,倒着输出它们的乘积. 输入 ...
- 某个应用的CPU使用率居然达到100%,我该怎么办?
> 本文是通过学习极客时间专栏<Linux性能优化实战>05 | 基础篇:某个应用的CPU使用率居然达到100%,我该怎么办? ## CPU 使用率 *** 为了维护 CPU 时间, ...
- 信息熵为什么要定义成-Σp*log(p)?
信息熵为什么要定义成-Σp*log(p)? 再解释信息熵之前,需要先来说说什么是信息量. 信息量是对信息的度量,单位一般用bit. 信息论之父克劳德·艾尔伍德·香农(Claude Elwood Sha ...
- (二)Angular+spring-security-cas前后端分离(基于ticket代码实现
一.前端实现 1.1.路由守卫(用于拦截路由认证) import { Injectable, Inject } from "@angular/core"; import { Can ...
- AcWing 240. 食物链 | 并查集
传送门 题目描述 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形. A吃B, B吃C,C吃A. 现有N个动物,以1-N编号. 每个动物都是A,B,C中的一种,但是我们并不知道它到底 ...
- react路由的跳转和传参
1.路由的跳转 一.DOM跳转 在需要跳转的页面导入import {Link} from 'react-router-dom',在需要跳转的地方使用link标签的to属性进行跳转,路由配置文件中导出的 ...