因为当时备考比较匆忙,图和树的题只是粗略写了一下并没有整理,包括搜索中的 BFS 也没整理,搜索和动态规划这两部分的内容较少,便直接整合到一起了,后续就没了,当时参加 PAT 是在大三,那时候总觉得时间用不完,写了几个没什么技术含量的小东西之后就觉得没意思了,当时刷知乎看到陈越姥姥说 PAT 就等于计算机的托福啊,便一头扎进去准备了,中间因为自身原因放下了一段时间,导致后来成绩不理想(当时还收到 58 同城的面试邀请),但功不唐捐感觉还是学到了不少东西
DFS
1103
题目:Integer Factorization
The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K−P factorization of N for any positive integers N, K and P.
Input Specification:
Each input file contains one test case which gives in a line the three positive integers N (≤400), K (≤N) and P (1<P≤7). The numbers in a line are separated by a space.
Output Specification:
For each case, if the solution exists, output in the format:
N = n[1]^P + … n[K]^P
where n[i]
(i
= 1, …, K
) is the i
-th factor. All the factors must be printed in non-increasing order.
Note: the solution may not be unique. For example, the 5-2 factorization of 169 has 9 solutions, such as 122+42+22+22+12, or 112+62+22+22+22, or more. You must output the one with the maximum sum of the factors. If there is a tie, the largest factor sequence must be chosen – sequence {a1, a2, ⋯, aK} is said to be larger than {b1, b2, ⋯, bK} if there exists 1≤L≤K such that ai=bi for i
If there is no solution, simple output Impossible
.
Sample Input 1:
169 5 2
Sample Output 1:
169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2
Sample Input 2:
169 167 3
Sample Output 2:
Impossible
思路: 输入初始数据之后,就将所有能够符合条件的因子放入数组中,然后搜索模拟
代码:
1 |
|
最大子序列和
1007
题目:Maximum Subsequence Sum
Given a sequence of K integers {N1, N2, …, NK}. A continuous subsequence is defined to be {Ni, Ni+1, …, Nj} where . The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence {-2, 11, -4, 13, -5, -2}, its maximum subsequence is {11, -4, 13} with the largest sum being 20.
Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.
Input Specification:
Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.
Output Specification:
For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.
Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4
思路: 使用动态规划的思想,将所有的子串和找出,求最大的即可
-
需要判断输入的数据是否为全负数,如果全是负数,就是从头到尾的输出
-
保留最大子串时需要判断前面的子串和与当前数相加是否更大,如果没有,保留当前数字即可,否者将其继续放入子串序列后
-
还需保存起始与结束位置,位置的存储也分情况判断,如果当前数加到子序列后,那么这条子串的起始下标还是之前
begin[i]=begin[i-1]
,如果从当前数开始开辟另一条子序列那么,当前的起始就是现在数字的下标begin[i]=i
代码:
1 |
|
背包问题
1068
题目:Find More Coins
Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she must pay the exact amount. Since she has as many as 104 coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find some coins to pay for it.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive numbers: N
(≤ , the total number of coins) and M
(≤ , the amount of money Eva has to pay). The second line contains N
face values of the coins, which are all positive numbers. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the face values V1≤V2≤⋯≤Vk such that V1+V2+⋯+Vk= M
. All the numbers must be separated by a space, and there must be no extra space at the end of the line. If such a solution is not unique, output the smallest sequence. If there is no solution, output “No Solution” instead.
Note: sequence {A[1], A[2], …} is said to be “smaller” than sequence {B[1], B[2], …} if there exists k≥1 such that A[i]=B[i] for all i<k, and A[k] < B[k].
Sample Input 1:
8 9
5 9 8 7 2 3 4 1
Sample Output 1:
1 3 5
Sample Input 2:
4 8
7 2 4 3
Sample Output 2:
No Solution
思路: 最后输出字典序最小的结果,先将其从大到小进行排序,将正确的值反向进行查找,跳转放入数组输出
代码:
1 |
|
- 本文标题:PAT 甲级 - 搜索和动规
- 本文作者:Aidan
- 创建时间:2021-08-15 22:23:06
- 本文链接:https://aidanblog.top/pat_level_a-search_and_dp/
- 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!