说明:增加了难度标明,纯属个人感觉,0-5难度依次增加
第一题:Conway’s Conjecture(难度:1.5)
题目:
John Horton Conway, a British mathematician active in recreational mathematics, proposed a conjecture in 2014: arrange the factors of any given number in ascending order, and pull the exponents down, we can get another number. Keep doing so we must end up at a prime number. For example:
18=2×3^2
232=2^3×29
2329=17×137
17137 is a prime.
Now you are supposed to write a program to make one step verification of this conjecture. That is, for any given positive integer N, you must factorize it, and then test if the number obtained from its factors is a prime.
By the way, this conjecture has been proven false by James Davis, who has discovered a counter example: 135323853961879=13×53^2×3853×96179. Alas …
Input Specification:
Each input file contains one test case which gives a positive integer N(<10^5)
Output Specification:
For each case, first print in a line the number obtained from N’s factors. The in the next line, print Yes if the above number is a prime, or No if not.
Sample Input 1:
2329
Sample Output 1:
17137
Yes
Sample Input 2:
124
Sample Output 2:
2231
No
Sample Input 3:
87516
Sample Output 3:
2232111317
No
Note:
本题类似1059,都是算质因子的,感觉今年3月无论PAT还是机试都能在往年真题找到一些影子,这也是一个刷题的方向吧
AC代码:
#include
using namespace std;
typedef long long ll;
vector ans,fac;
ll n;
string str;
bool isprime(ll a)
{
if(a<=1) return false;
ll sqr=(ll)sqrt(1.0*a);
for(ll i=2;i<=sqr;i++) if(a%i==0) return false;
return true;
}
int main()
{
cin>>n;
if(n==1||n==0) { printf("%lld\nNo",n); return 0; }
for(ll i=2;i<=n;i++) if(isprime(i)) fac.push_back(i);
for(int i=0;i0) ans.push_back(fac[i]);
if(cnt>1) ans.push_back(cnt);
}
for(int i=0;i
第二题:Play with Linked List(难度:1.8)
题目: Then N lines follow, each describes a node in the format: Address Data Next Output Specification: Sample Input: Note: AC代码: 附赠一个晴神模拟题,这个就是真题本题了 题目: 输入: 输出: Sample Input Sample Output 参考代码: 第三题:Unsuccessful Searches(难度:1.5) 题目:The above figure is a question from GRE-CS 2018. It states: Given an initially empty hash table HT of size 11. The hash function is H(key)=key%7, with linear probing used to resolve the collisions. Now hash the keys 87, 40, 30, 6, 11, 22, 98 and 20 one by one into HT. What is the average search time for unsuccessful searches? The answer is 6. Now you are supposed to write a program to solve this kind of problems. Input Specification: Output Specification: Sample Input 1: 小插曲: AC代码: 第四题:Ambulance Dispatch(难度:4.0) 题目: In case your options are not unique, inform the one with the largest number of ambulances available. If there is still a tie, choose the one that can pass the least number of streets to reach the spot, which is guaranteed to be unique. Input Specification: The next line gives Na non-negative integers, where the i-th integer is the number of available ambulances at the i-th center. All the integers are no larger than 100. In the next line a positive number M (≤10^4) is given as the number of streets connecting the spots and the centers. Then M lines follow, each describes a street by giving the indices of the spots or centers at the two ends, followed by the time taken to pass this street, which is a positive integer no larger than 100. Finally the number of emergency calls, K, is given as a positive integer no larger than 10^3, followed by K indices of pick-up spots. All the inputs in a line are separated by a space. Output Specification: Sample Input: Note: AC代码:
Given a singly linked list L1→L2→⋯→Ln−1 →Ln and an integer 1≤k
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤10^5) which is the total number of nodes, and an integer 1≤k
where Address is the position of the node, Data is a positive integer no more than 10^5
, and Next is the position of the next node. It is guaranteed that there are at least two nodes on the list.
For each case, output in order the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
Sample Output:
00000 4 68237
68237 6 33218
33218 3 99999
99999 5 12309
12309 2 00100
00100 1 -1
本题是2019年408数据结构真题改编的,每年3月机试都会有当年408算法题的,所以准备三月份机试的大佬们要注意哦,我写的略微麻烦,欢迎有更好想法的大佬指正#include
给定一条单链表,将链表结点进行头尾交错重新排序,即如果一条单链表为 L1 -> L2 -> … -> L(n-1) -> Ln ,那么重新排序完的结果是 L1 -> Ln -> L2 -> L(n-1) -> L3 -> L(n-2) -> …
每个输入文件中一组数据。
第一行给出结点的总个数N(0
其中Address为结点地址(不足5位的高位用零填充至5位),Data为结点的数据域(绝对值不超过10^5的整数),Next为结点的指针域(即下一个结点的地址)。数据保证Address不等于-1。
输出按题目要求重新排序后的单链表。第一行为重新排序后单链表上结点的个数、第一个结点的地址。
之后每行一个结点,输出格式与输入相同,结点输出顺序为单链表连接顺序。
5 11111
33333 0 44444
22222 2 33333
11111 5 22222
05689 8 -1
44444 6 05689
5 11111
11111 5 05689
05689 8 22222
22222 2 44444
44444 6 33333
33333 0 -1#include
Each input file contains one test case. For each case, the first line gives 3 positive integers TSize (≤10^3, the table size), M (≤TSize, the divisor in the hash function), and N (≤TSize, the number of integers to be inserted). Then N non-negative integers (≤10 ^4) are given in the next line, separated by spaces.
Print in a line the average search time for unsuccessful searches, after hashing the N integers into the table. The answer must be accurate up to 1 decimal place.
11 7 8
87 40 30 6 11 22 98 20
Sample Output 1:
6.0
Sample Input 2:
3 3 3
81 2 5
Sample Output 2:
4.0
Note: In sample 2, the last test of the original position counts as well.
这道题并不难,但是DS忘完了,刚开始手算都不会,有点惨。重新温习了一下DS中查找的部分一次性AC了(尴尬.ipg)#include
Given the map of a city, with all the ambulance dispatch centers (救护车派遣中心) and all the pick-up spots marked. You are supposed to write a program to process the emergency calls. It is assumed that the callers are waiting at some pick-up spot. You must inform the nearest (that is, to take the minimum time to reach the spot) dispatch center if that center has at least one ambulance available. Note: a center without any ambulance must not be considered.
Each input file contains one test case. For each case, the first line contains two positive integers Ns(≤10^3) and Na(≤10), which are the total number of pick-up spots and the number of ambulance dispatch centers, respectively. Hence the pick-up spots are numbered from 1 to Ns, and the ambulance dispatch centers are numbered from A−1 to A−Na .
For each of the K calls, first print in a line the path from the center that must send an ambulance to the calling spot. All the nodes must be separated by exactly one space and there must be no extra space at the beginning or the end of the line. Then print the minimum time taken to reach the spot in the next line. It is assumed that the center will send an ambulance after each call. If no ambulance is available, just print All Busy in a line. It is guaranteed that all the spots are connected to all the centers.
7 3
3 2 2
16
A-1 2 4
A-1 3 2
3 A-2 1
4 A-3 1
A-1 4 3
6 7 1
1 7 3
1 3 3
3 4 1
6 A-3 5
6 5 2
5 7 1
A-2 7 5
A-2 1 1
3 5 1
5 A-3 2
8
6 7 5 4 6 4 3 2
Sample Output:
A-3 5 6
4
A-2 3 5 7
3
A-3 5
2
A-2 3 4
2
A-1 3 5 6
5
A-1 4
3
A-1 3
2
All Busy
有点难,跟1072加油站的那道题目有一些相像,都是循环Dijkstra,不同的是本题需要保存每一个急救中心到到出事地点的路径,最开始想到的添加超级源点,每查询一个出事点就做Na个Dijkstra,有一个点超时,后来Floyd算法还是超时(其实1000个点根本不可能Floyd),最后参考了一位大佬的代码恍然大悟。。。。。#include