今天实验室的师兄做微软实习编程题,我也来做一做吧。如有错误,请见谅,望及时指正。
Question #1
Playing with Beads
There are N people in a group labeled form 1 to N. People are connected to each other via threads in the following manner:
The person with label number K is connected to all the persons with label J such that J exactly divides K. Beads can be passed through the threads. If a person P has a head, in how many ways can the bead be passed in the network of threads so that it returns to the same person within X moves or less.
Move: Passing the bead from one person to the orther.
Input Specification:
Input1: N, denoting the number of people.
Input2: P, label of the person having the bead.
Input3: X, maximum number of moves that can be made.
Output Specification:
Your function should return the total number of ways in which the bead will return to its initial position within X moves.
Example:
Input1: 3
Input2: 2
Input3: 2
Output: 1
分析一波:邻接矩阵n次方的几何意义。
# 构造n×n的零阵
def matrix(n):
return [[0 for _ in range(n)] for _ in range(n)]
# 定义矩阵乘法
def dot(A, B):
row_len = len(A)
column_len = len(B[0])
cross_len = len(B)
res_mat = matrix(column_len)
for i in range(row_len):
for j in range(column_len):
for k in range(cross_len):
temp = A[i][k] * B[k][j]
res_mat[i][j] += temp
return res_mat
def maxCircless(input1, input2, input3):
m = matrix(input1)
# 构造邻接矩阵
for i in range(input1):
for j in range(input1):
if (i+1) % (j+1) == 0:
if i == j:
pass
else:
m[i][j] = 1
m[j][i] = 1
num = 0
sumt = m
# 利用邻接矩阵n次方的几何意义
for x in range(input3 - 1):
sumt = dot(sumt, m)
num += sumt[input2][input2]
return num
if __name__ == "__main__":
print(maxCircless(3, 2, 4))
print(maxCircless(3, 2, 2))
Question #2
Archer
Ralph is a brilliant archer who always wines the archery. This time, to challenge him, the jubges have set a new type of challenge.
There are N bottles kept in a row with each bootle given a specific number. Ralph has to target and hit the bottles in a very interesting way. In one shot, Ralph can remove 1 or more than 1 bottles such that the numbers present on these sequence of bottlers from a palindrome. Also, these bottles must be present one after the other. After taking this shot, the remaining bottles are shifted so that all the bottles are again in a row and Ralph shoots the bottles again.
Now, if Ralph gets 1 point for each shot, find the minimum number of points Ralph can score.
Input Specification:
Input1: The number of bottles N.
Input2: The array representing the numbers written on the bottles with A[i] representing the number on the i-th bottles.
Output Specification:
The minimum number of points Ralph can score.
Example1:
Input1: 2
Input2: {1, 2}
Output: 2
Example2:
Input1: 5
Input2: {1, 4, 3, 1, 5}
Output: 3
分析:占位符
def dns(p):
if len(p) == 0:
return 0
max_i = max(x[0] for x in p)
min_j = min(x[1] for x in p)
num = []
for x in p:
if x[0] == max_i or x[1] == min_j:
num.append(dns([t for t in p if t[0] < x[0] and t[1] > x[1]]) + 1)
return max(k for k in num)
def f(A, n):
# 在左下矩阵,记录不同位置数字相同时的坐标
flag = [(str(i), str(j)) for i in range(n) for j in range(n) if i > j and A[i] == A[j]]
# 按照行升序排序,列降序排序(左下角优先级最高)
flag.sort(key=lambda item: (item[0], tuple(map(lambda x: -ord(x), item[1]))), reverse=True)
# 贪心选择
sum = dns([(int(x[0]), int(x[1])) for x in flag])
return n - 2 * sum
if __name__ == "__main__":
a = [1, 2]
b = [1, 4, 3, 1, 5]
print(f(a, 2))
print(f(b, 5))
Question #3
占位符
Question #4
占位符