7-1 Sexy Primes

7-1 Sexy Primes
分数 20
作者 陈越
单位 浙江大学Sexy primes are pairs of primes of the form (p, p+6),
so-named since "sex" is the Latin word for "six".
(Quoted from http://mathworld.wolfram.com/SexyPrimes.html)Now given an integer, you are supposed to tell if it is a sexy prime.
Input Specification:Each input file contains one test case. Each case gives a positive integer N (≤108).
Output Specification:For each case, print in a line Yes if N is a sexy prime, then print in the next line the other sexy prime
paired with N (if the answer is not unique, output the smaller number). Or if N is not a sexy prime,
print No instead, then print in the next line the smallest sexy prime which is larger than N.
Sample Input 1:47Sample Output 1:Yes
41Sample Input 2:21Sample Output 2:No
23代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB

代码展示:

# Sexy Primes
import math
def isPrime(N):if N <= 1:return Falsefor i in range(2, int(math.sqrt(N)) + 1):if N % i == 0:return Falsereturn Truedef isSexPrime(N):if isPrime(N):if isPrime(N-6) or isPrime(N+6):return Trueelse:return Falseif __name__ == '__main__':N = int(input().strip())if isSexPrime(N):print('Yes')if isPrime(N-6):print(N-6)else:print(N+6)else:print('No')while True:N += 1if isSexPrime(N):print(N)break


7-2 Anniversary

7-2 Anniversary
分数 25
作者 陈越
单位 浙江大学Zhejiang University is about to celebrate her 122th anniversary in 2019.
To prepare for the celebration, the alumni association (校友会) has gathered
the ID's of all her alumni. Now your job is to write a program to count the
number of alumni among all the people who come to the celebration.
Input Specification:Each input file contains one test case. For each case, the first part is about the information of all
the alumni. Given in the first line is a positive integer N (≤105). Then N lines follow, each
contains an ID number of an alumnus. An ID number is a string of 18 digits or the letter X.
It is guaranteed that all the ID's are distinct.The next part gives the information of all the people who come to the celebration.
Again given in the first line is a positive integer M (≤105). Then M lines follow, each
contains an ID number of a guest. It is guaranteed that all the ID's are distinct.
Output Specification:First print in a line the number of alumni among all the people who come to the celebration.
Then in the second line, print the ID of the oldest alumnus -- notice that the 7th - 14th digits of
the ID gives one's birth date. If no alumnus comes, output the ID of the oldest guest instead.
It is guaranteed that such an alumnus or guest is unique.
Sample Input:5
372928196906118710
610481197806202213
440684198612150417
13072819571002001X
150702193604190912
6
530125197901260019
150702193604190912
220221196701020034
610481197806202213
440684198612150417
370205198709275042Sample Output:3
150702193604190912代码长度限制
16 KB
时间限制
800 ms
内存限制
64 MB


代码展示:

# Anniversary
N = int(input().strip())  # 校友数量
alumni = set()
for i in range(N):alumnus = input().strip()alumni.add(alumnus)     M = int(input().strip())  # 访客数量
# 372928196906118710cnt = 0  # 记录参会校友数量
oldestGuest, oldestAlumnus = 'z'*18, 'z'*18
for _ in range(M):guest = input().strip()if guest in alumni:cnt += 1if guest[6:13] < oldestAlumnus[6:13]:oldestAlumnus = guestelse:if guest[6:13] < oldestGuest[6:13]:oldestGuest = guest
print(cnt)
if cnt == 0:print(oldestGuest)
else:print(oldestAlumnus)


7-3 Telefraud Detection

7-3 Telefraud Detection
分数 25
作者 陈越
单位 浙江大学Telefraud(电信诈骗) remains a common and persistent problem in our society.
In some cases, unsuspecting victims lose their entire life savings. To stop this crime,
you are supposed to write a program to detect those suspects from a huge amount of
phone call records.A person must be detected as a suspect if he/she makes more than K short phone calls
to different people everyday, but no more than 20% of these people would call back.
And more, if two suspects are calling each other, we say they might belong to the same gang.
A makes a short phone call to B means that the total duration of the calls from A to B is
no more than 5 minutes.
Input Specification:Each input file contains one test case. For each case, the first line gives 3 positive
integers K (≤500, the threshold(阈值) of the amount of short phone calls), N (≤103,
the number of different phone numbers), and M (≤105, the number of phone call records).
Then M lines of one day's records are given, each in the format:caller receiver durationwhere caller and receiver are numbered from 1 to N, and duration is no more than 1440 minutes
in a day.
Output Specification:Print in each line all the detected suspects in a gang, in ascending order of their numbers.
The gangs are printed in ascending order of their first members. The numbers in a line
must be separated by exactly 1 space, and there must be no extra space at the beginning
or the end of the line.If no one is detected, output None instead.
Sample Input 1:5 15 31
1 4 2
1 5 2
1 5 4
1 7 5
1 8 3
1 9 1
1 6 5
1 15 2
1 15 5
3 2 2
3 5 15
3 13 1
3 12 1
3 14 1
3 10 2
3 11 5
5 2 1
5 3 10
5 1 1
5 7 2
5 6 1
5 13 4
5 15 1
11 10 5
12 14 1
6 1 1
6 9 2
6 10 5
6 11 2
6 12 1
6 13 1Sample Output 1:3 5
6Note: In sample 1, although 1 had 9 records, but there were 7 distinct receivers, among
which 5 and 15 both had conversations lasted more than 5 minutes in total. Hence 1 had
made 5 short phone calls and didn't exceed the threshold 5, and therefore is not a suspect.
Sample Input 2:5 7 8
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
2 1 1
3 1 1Sample Output 2:None代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB

代码展示:(运行超时)

# 7_3 Telefraud Detection
# import collections
K, N, M = map(int, input().strip().split())CallRecordDict = dict()
for _ in range(M):caller, receiver, duration = map(eval, input().strip().split())if caller == receiver:continueCallRecordDict[(caller, receiver)] = CallRecordDict.get((caller, receiver), 0) + duration
#########
shortReceiverDict = dict()
for call, duration in CallRecordDict.items():if duration > 5:  # 长于五分钟的不视为短通话continuecaller, receiver = callif caller not in shortReceiverDict:shortReceiverDict[caller] = [receiver]else:shortReceiverDict[caller].append(receiver)suspects = list()
for caller, receivers in shortReceiverDict.items():shortCallNum = len(receivers)if shortCallNum <= K:  # 短通话数量必须超过Kcontinuecallback_cnt = 0for recv in receivers:if (recv, caller) in CallRecordDict:callback_cnt += 1if callback_cnt <= shortCallNum / 5:suspects.append(caller)suspectsNum = len(suspects)
if suspectsNum == 0:print("None")    quit()HeightsDict, RootsDict = {i : 1 for i in suspects}, {j : j for j in suspects}def getRoot(RootsDict,child):if RootsDict[child] == child:return childelse:root = getRoot(RootsDict, RootsDict[child])RootsDict[child] = rootreturn rootfor i in range(suspectsNum):for j in range(i+1, suspectsNum):gangA, gangB = suspects[i], suspects[j]if ((gangA, gangB) in CallRecordDict) and \((gangB, gangA) in CallRecordDict):  # 两个嫌疑犯相互通话rootA, rootB = getRoot(RootsDict,gangA), getRoot(RootsDict,gangB)if rootA == rootB:continueHeightA, HeightB = HeightsDict[rootA], HeightsDict[rootB]if HeightA > HeightB:RootsDict[rootB] = rootAelif HeightB > HeightA:RootsDict[rootA] = rootBelse:RootsDict[rootB] = rootAHeightsDict[rootA] += 1gangsDict = dict()
for suspect in suspects:root = getRoot(RootsDict, suspect)if root not in gangsDict:gangsDict[root] = [suspect]else:gangsDict[root].append(suspect)result = list()
for k, v in gangsDict.items():v.sort()result.append(v)result.sort()
for item in result:print(*item)


7-4 Structure of a Binary Tree

7-4 Structure of a Binary Tree
分数 30
作者 陈越
单位 浙江大学Suppose that all the keys in a binary tree are distinct positive integers. Given the
postorder and inorder traversal sequences, a binary tree can be uniquely determined.Now given a sequence of statements about the structure of the resulting tree,
you are supposed to tell if they are correct or not. A statment is one of the following:A is the rootA and B are siblingsA is the parent of BA is the left child of BA is the right child of BA and B are on the same levelIt is a full treeNote:Two nodes are on the same level, means that they have the same depth.A full binary tree is a tree in which every node other than the leaves has two children.Input Specification:Each input file contains one test case. For each case, the first line gives a positive
integer N (≤30), the total number of nodes in the binary tree. The second line gives the
postorder sequence and the third line gives the inorder sequence. All the numbers in a
line are no more than 103 and are separated by a space.Then another positive integer M (≤30) is given, followed by M lines of statements. It is
guaranteed that both A and B in the statements are in the tree.
Output Specification:For each statement, print in a line Yes if it is correct, or No if not.
Sample Input:9
16 7 11 32 28 2 23 8 15
16 23 7 32 11 2 28 15 8
7
15 is the root
8 and 2 are siblings
32 is the parent of 11
23 is the left child of 16
28 is the right child of 2
7 and 11 are on the same level
It is a full treeSample Output:Yes
No
Yes
No
Yes
Yes
Yes代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB



代码展示:

# 7-4 Structure of a Binary Treeclass TreeNode():def __init__(self, data):self.data = dataself.depth = Noneself.leftChild, self.rightChild = None, Nonedef preOrder(self):print(self.data, end=' ')if self.leftChild != None:self.leftChild.preOrder()if self.rightChild != None:self.rightChild.preOrder()def inOrder(self):if self.leftChild != None:self.leftChild.inOrder()print(self.data, end=' ')    if self.rightChild != None:self.rightChild.inOrder()def postOrder(self):if self.leftChild != None:self.leftChild.postOrder()if self.rightChild != None:self.rightChild.postOrder()print(self.data, end=' ')def addIntoDict(self, treeDict):treeDict[self.data] = selfif self.leftChild != None:self.leftChild.addIntoDict(treeDict)if self.rightChild != None:self.rightChild.addIntoDict(treeDict)def setParent(self, parent):self.parent = parentif self.leftChild != None:self.leftChild.setParent(self)if self.rightChild != None:self.rightChild.setParent(self)def setDepth(self,depth):self.depth = depthif self.rightChild != None:self.rightChild.setDepth(depth+1)if self.leftChild != None:self.leftChild.setDepth(depth+1)def isFullTree(self):if self.rightChild == None == self.leftChild:return Trueelif self.rightChild != None and self.leftChild == None:return Falseelif self.rightChild == None and self.leftChild != None:return Falseelse:  # 左右子树均不为空if self.rightChild.isFullTree() and self.leftChild.isFullTree():return Trueelse:return Falsedef isRoot(self):return self.parent == Nonedef isSiblings(self, other):if self.parent != None:if self.parent == other.parent:return Truereturn Falsedef buildTree(PostSeq, InSeq):if len(PostSeq) == 0:return Nonedata = PostSeq[-1]index = InSeq.index(data)tree = TreeNode(data=data)tree.leftChild = buildTree(PostSeq[:index], InSeq[:index])tree.rightChild = buildTree(PostSeq[index:-1], InSeq[1+index:])return treeN = int(input().strip())
PostSeq = list(map(int, input().strip().split()))
InSeq = list(map(int, input().strip().split()))
tree = buildTree(PostSeq=PostSeq, InSeq=InSeq)
TreeDict = dict()
tree.addIntoDict(TreeDict)
tree.setParent(None)
tree.setDepth(1)M = int(input().strip())for _ in range(M):line = input().strip()if 'root' in line:A = int(line.split()[0])if TreeDict[A].isRoot():print("Yes")else:print("No")elif 'siblings' in line:ls = line.split()A, B = TreeDict[int(ls[0])], TreeDict[int(ls[2])]if A.isSiblings(B):print("Yes")else:print("No")elif 'parent' in line:ls = line.split()A, B = TreeDict[int(ls[0])], TreeDict[int(ls[-1])]if A == B.parent:print("Yes")else:print("No")elif 'left' in line:ls = line.split()A, B = TreeDict[int(ls[0])], TreeDict[int(ls[-1])]if A == B.leftChild:print("Yes")else:print("No")elif 'right' in line:ls = line.split()A, B = TreeDict[int(ls[0])], TreeDict[int(ls[-1])]if A == B.rightChild:print("Yes")else:print("No")elif 'level' in line:ls = line.split()A, B = TreeDict[int(ls[0])], TreeDict[int(ls[2])]if A.depth == B.depth:print("Yes")else:print("No")elif 'full' in line:if tree.isFullTree():print("Yes")else:print("No")

PAT(甲级)2019春季考试(Python实现)相关推荐

  1. PAT甲级线上考试备考

    我本人刷了两遍题库 之前考了两次都是四十几分,我认为目前甲级题库已经过时了 (对于有算法基础的同学来说) 前100题没有做的必要 除非是典型算法. DFS 和 回溯是相当重要的考点,如果有时间的话一定 ...

  2. PAT甲级 2019年冬季 题解

    题目1:7-1 Good in C (20分) When your interviewer asks you to write "Hello World" using C, can ...

  3. 2019秋季PAT甲级考试总结:努力+策略+运气

    鉴于这两天有很多网友联系我问这次考试的题解,所以我干脆就花点时间把C++题解整理出来了,见文末 经过一两个月的备战PAT,在今天终于画上了一个圆满的句号,取得了满分的成绩. 我是在南京的金陵科技学院考 ...

  4. 2019 PAT甲级秋季考试总结

    因为要考研浙大软院,所以考个PAT甲级抵机试是很好的!而且之前天梯赛金奖发了150的代金券,让原价256的考试看起来也没那么贵了~于是很早就报名啦!(但是很后悔3月份那场没报,不然就可以更早轻松一点了 ...

  5. 2021年春季PAT甲级考试

    作为一个绩点不高.牌子不硬的退役ACMer,去年冬天在nvpy的鼓励下,决定先试试PAT为考研做准备,于是认认真真把所有20分的题目过了一遍.放寒假以后,就开始练习所有25分的题目,并且每道题做完都总 ...

  6. 2019年秋季PAT甲级考试记录、经验总结

    在武汉科技大学黄家湖校区考的,非常偏远,在很荒僻的地方,荒僻到公交都要3元的地方(开玩笑~).我去的路上大概花了两个小时,12点半到的,武科机房软.硬件条件很好,跟官网的要求一样,绝对能够满足各种常用 ...

  7. pat甲级考试报名费_PAT(甲级)2019年冬季考试 题解

    总结写在前头: 考了字符串.链表(伪链表).图.树.考点比较均衡. 本次考试难度还行,需要较扎实的数据结构底子,对于字符串处理的技巧有一定的要求. 说句题外话,字符串是个重点,主要因为很多人会忽视字符 ...

  8. PAT学习资料汇总(PAT甲级、PAT顶级、PAT考试经验)

    二.PAT甲级 PAT甲级真题目录(按题型整理) PAT甲级真题目录(按题型整理)_love music.的博客-CSDN博客_pat甲级真题 PAT甲[所有题目+解析+代码示例+总结]附带所有历年整 ...

  9. PAT/PTA甲级2020春季题目【满分】弃坑贴

    开局上张图: 更一下题目通过率,有点吃惊... PAT甲级考纲:(官网目前已经删除了,以前是有的,见下文) 先说一下今天考试的整体情况: 因为这是第一次线上考试,导致很多地方包括很多同学的网络,设备等 ...

最新文章

  1. python基础---闭包、装饰器
  2. 第 5-7 课:Java 中的各种锁和 CAS + 面试题
  3. saltstack计划任务工具和其他命令
  4. 算法与数据结构(一)-导学
  5. zebradesigner2教程_斑马条码打印机动安装及ZebraDesigner操作指导.doc
  6. 在计算机知识post的含义,计算机术语POST是指的什么意思?
  7. 英语情景对话计算机专业,工作有关情景对话英语
  8. 自动量程万用表的实现原理_自动量程万用表设计方案
  9. 电脑连wifi老是断断续续的怎么回事
  10. 为什么一个操作系统连个进程启动日志都没有
  11. Gym 101778G
  12. 仿微博系统数据库设计和er图设计
  13. AttributeError: module ‘torch‘ has no attribute ‘inference_mode‘
  14. 嵌入式开发——rtc时钟调试笔记
  15. 【产品设计】塑胶模具设计:合模线/分模线(PL - Parting line)详解
  16. 初学乍练redis:Redis 5 Cluster手工指定主从关系
  17. java中强耦合是什么意思_什么是Java程序的耦合
  18. 如何搭建一个独立博客——简明Github Pages与Hexo教程
  19. Linux基本认识及环境搭建
  20. 深入理解EMA和SMA

热门文章

  1. 计算机组成原理cop乘法器,计算机组成原理课程设计报告COP2000实现乘法器和除法器.doc...
  2. sdddddddddddddddddddddd
  3. FZ操场(数学,推公式)
  4. macOS如何添加Root用户
  5. 本地项目上传公司GitLab步骤
  6. 图片上传预览,解决路径为fakepath
  7. 如何用SQL统计用户复购(or留存)数据
  8. 弘辽科技:中小卖家如何处理店铺中差评,做好评论营销很重要
  9. Endnote X9收藏网址--安装、使用
  10. virtualbox无法打开虚拟机,error in supR3HardenedWinReSpawn终极解决方法