• PAT(甲级)2022年夏季考试


    7-1 What Day is Today(20分)

    Amy and Tom are arguing about what day is today. Their conversations are as the following:

    1. Amy: Today is Friday.
    2. Tom: No! Today is Saturday.
    3. Amy: But Wednesday was yesterday.
    4. Tom: No way! Yesterday was Thursday.
    5. Amy: Then tomorrow must be Tuesday.
    6. Tom: Are you kidding? It's Monday tomorrow.

    Now their mother tells you that each of them has said only one thing correct. It's your job to tell exactly what day is today.

    Input Specification:

    Each input file contains one test case. Each case consists of 2 lines, each gives the claims of a kid, in the following format:

    yesterday today tomorrow
    

    where the days are represented by numbers from 0 to 6, corresponding to Sunday through Saturday.

    Output Specification:

    First output in a line the English name for "today". It is guaranteed that each kid has said only one thing correct, and there is a unique answer for "today".

    Then in the next two lines, print in order the correct days (either yesterday or today, or tomorrow) obtained from the kids.

    Sample Input:

    1. 3 5 2
    2. 4 6 1

    Sample Output:

    1. Friday
    2. today
    3. yesterday

    Note: The English names for the days are

    1. 0 - Sunday
    2. 1 - Monday
    3. 2 - Tuesday
    4. 3 - Wednesday
    5. 4 - Thursday
    6. 5 - Friday
    7. 6 - Saturday

    代码(Python3): 

    1. pre = [6, 0, 1, 2, 3, 4, 5]
    2. next = [1, 2, 3, 4, 5, 6, 0]
    3. day = ["yesterday", "today", "tomorrow"]
    4. week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    5. a = list(map(int, input().split()))
    6. b = list(map(int, input().split()))
    7. for i in range(7):
    8. c1 = 0
    9. c2 = 0
    10. if a[0] == pre[i]:
    11. c1 += 1
    12. r1 = 0
    13. if a[1] == i:
    14. c1 += 1
    15. r1 = 1
    16. if a[2] == next[i]:
    17. c1 += 1
    18. r1 = 2
    19. if b[0] == pre[i]:
    20. c2 += 1
    21. r2 = 0
    22. if b[1] == i:
    23. c2 += 1
    24. r2 = 1
    25. if b[2] == next[i]:
    26. c2 += 1
    27. r2 = 2
    28. if c1 == 1 and c2 == 1:
    29. print(week[i])
    30. print(day[r1])
    31. print(day[r2])
    32. break

    7-2 Least Recently Used Cache(25分)

    Least Recently Used (LRU) cache scheme is to remove the least recently used frame (the one hasn't been used for the longest amount of time) when the cache is full and a new page is referenced which is not there in cache.

    Your job is to implement this LRU cache scheme.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives 2 positive integers N (≤104) and M (≤105) which are the size of the cache and the number of referenced page ID's. Then M referenced page ID's are given in the next line. A page ID is a number in the range [1,2×104]. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, output in a line the page ID's in the order of their being removed from the cache. All the numbers in a line must be separated by 1 space, and there must be no extra space at the beginning or the end of the line.

    It is guaranteed that at least one page will be removed.

    Sample Input:

    1. 4 11
    2. 1 2 3 1 4 5 2 1 5 6 3

    Sample Output:

    2 3 4 2
    

    Hint:

    When pages with ID's 1, 2, and 3 comes in order, they are all stored in the cache and page 1 is now the LRU frame.

    When page 1 is accessed, page 2 then becomes the LRU frame.

    Page 4 can still be stored in the cache, and now the cache is full. When page 5 is to be cached, page 2 will be removed and page 3 becomes the LRU frame.

    When the next page 2 comes in, page 3 is removed and page 1 becomes the LRU frame.

    Then page 1 is accessed, so page 4 becomes the LRU frame.

    When page 5 is accessed, the LRU frame is not changed. Then page 6 comes in and page 4 is removed. The LRU frame is now page 2.

    Finally page 2 is removed when page 3 comes in.

    提交结果:

    代码(Python3): 

    1. n, m = list(map(int, input().split()))
    2. data2 = list(map(int, input().split()))
    3. c = 0
    4. q = []
    5. num = {}
    6. v = []
    7. r = []
    8. for x in data2:
    9. num[x] = 0
    10. for x in data2:
    11. if c < n:
    12. q.append(x)
    13. if num[x] == 0:
    14. c += 1
    15. num[x] += 1
    16. else:
    17. if num[x] == 0:
    18. while num[q[0]] != 1:
    19. num[q[0]] -= 1
    20. q.pop(0)
    21. num[q[0]] -= 1
    22. r.append(str(q[0]))
    23. q.pop(0)
    24. q.append(x)
    25. num[x] += 1
    26. print(" ".join(r))

    7-3 DFS Sequence(25分)

    The above question is commonly seen in a final exam of the course Data Structures. That is, given a graph G, you are supposed to tell if a sequence of vertices can be possibly obtained from any depth first search (DFS) on G.

    Now it is your job to write a program which can give the answers automatically.

    Note: it is required that each vertex must be visited once even if the graph is not connected. In the graph given by the sample input, if we start from vertex 4, then vertex 1 cannot be reached during this DFS, yet it can be the first vertex visited in the next DFS. Hence the 5th query 4 3 2 5 1 is possible.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains 3 positive integers: N (≤103), M (≤104), and K (≤100), which are the number of vertices, the number of edges, and the number of queries, respectively. Then M lines follow, each gives an edge in the format:

    source destination
    

    which means that there is an edge from source vertex to destination vertex. Here we assume that all the vertices are numbered from 1 to N. It is guaranteed that source is never the same as destination.

    Finally K lines of queries are given, each contains N vertices. The numbers in a line are separated by spaces.

    Output Specification:

    For each query, print in a line yes if the given sequence does correspond to a DFS sequence, or no if not.

    Sample Input:

    1. 5 7 6
    2. 1 2
    3. 1 3
    4. 1 5
    5. 2 5
    6. 3 2
    7. 4 3
    8. 5 4
    9. 1 5 4 3 2
    10. 1 3 2 5 4
    11. 1 2 5 4 3
    12. 1 2 3 4 5
    13. 4 3 2 5 1
    14. 5 4 3 2 5

    Sample Output:

    1. yes
    2. yes
    3. yes
    4. no
    5. yes
    6. no

    提交结果:

     代码(Python3):

    1. def dfs(x):
    2. global flag
    3. if flag == 0 or q == []:
    4. return
    5. vis[x] = 1
    6. while q != []:
    7. if a[x - 1][q[0] - 1] == 1:
    8. y = q[0]
    9. q.pop(0)
    10. dfs(y)
    11. else:
    12. for i in range(len(vec[x])):
    13. if vis[vec[x][i]] == 0:
    14. flag = 0
    15. return
    16. return
    17. vis = {}
    18. vec = {}
    19. n, m, k = list(map(int, input().split()))
    20. a = [[0 for j in range(n)] for i in range(n)]
    21. for i in range(m):
    22. x, y = list(map(int, input().split()))
    23. a[x - 1][y - 1] = 1
    24. if x not in vec.keys():
    25. vec[x] = [y]
    26. else:
    27. vec[x].append(y)
    28. for i in range(k):
    29. flag = 1
    30. q = list(map(int, input().split()))
    31. s = set(q)
    32. for x in q:
    33. vis[x] = 0
    34. while q != []:
    35. y = q[0]
    36. q.pop(0)
    37. dfs(y)
    38. if flag == 0 or len(s) != n:
    39. print("no")
    40. else:
    41. print("yes")

    7-4 Complete D-Tree(30分)

    A d-tree is a tree of degree d. A complete tree is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

    Given the pre-order traversal sequence of a complete d-tree, you are supposed to obtain its level-order traversal sequence. And more, for any given node, you must be able to output the bottom-up path from this node to the root.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives two positive integers: N (≤50) and d (2≤d≤5), which are the total number of nodes in the tree, and the tree's degree, respectively. Then in the next line, N integer keys (≤100) are given as the pre-order traversal sequence of the tree. Finally K (≤N) and K positions are given, where each position is the index of a key in the level-order traversal sequence, starting from 0.

    All the numbers in a line are separated by a space.

    Output Specification:

    For each case, first print in one line the level-order traversal sequence of the complete d-tree. Then for each given position, print in a line the bottom-up path from this node to the root.

    All the numbers in a line must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line.

    Sample Input:

    1. 9 3
    2. 91 71 2 34 10 15 55 18 7
    3. 3
    4. 5 7 3

    Sample Output:

    1. 91 71 15 7 2 34 10 55 18
    2. 34 71 91
    3. 55 15 91
    4. 7 91

     代码(Python3):

    1. def dfs(x):
    2. if x >= n:
    3. return
    4. le[x] = pre[0]
    5. pre.pop(0)
    6. for i in range(1, d + 1):
    7. dfs(x * d + i)
    8. le = {}
    9. n, d = list(map(int, input().split()))
    10. pre = list(map(int, input().split()))
    11. dfs(0)
    12. for i in range(n):
    13. print(le[i], end="")
    14. if i < n - 1:
    15. print(" ", end="")
    16. else:
    17. print()
    18. k = int(input())
    19. data = list(map(int, input().split()))
    20. for x in data:
    21. while x:
    22. print(le[x], end=" ")
    23. x = int((x - 1) / d)
    24. print(le[0])
  • 相关阅读:
    多壁碳纳米管-室温离子液体1-丁基-3-甲基咪唑六氟磷酸盐([EMIM]PF6)修饰辣根过氧化物酶(HRP)|新型碳糊酶电极(HRP-MWCNTs-CILE)
    江科大STM32 终
    云租赁安全管理制度
    新课程发布 | 如何用 7 分钟击破 Serverless 落地难点?
    [UE虚幻引擎插件介绍] DTSQLite 插件说明 :蓝图操作SQLite3文件,执行SQL语句。
    如何利用数字创新使您的企业脱颖而出
    【剑指offer33】二叉搜索树的后序遍历序列
    763. Partition Labels
    如果后端返回了十万条数据要你插入到页面中,你会怎么处理?
    [Python] reverse()函数 VS reversed()函数
  • 原文地址:https://blog.csdn.net/weixin_55730361/article/details/126572608