按照以下要求写出python代码,Suppose there are n assignments A= [a1, a2 …ai …an]that you need to complete before the deadline. An assignment ai= [durationi, deadlinei] need durationi days to complete and must be completed before or on deadlinei. You can only do one assignment at a time and start next assignment once the current assignment is completed. Assuming you start on day 1, implement an efficient algorithm to find the maximum number of assignments you can complete.
时间: 2023-04-09 20:03:40 浏览: 120
以下是Python代码:
def max_assignments(A):
# 按照deadlinei从小到大排序
A.sort(key=lambda x: x[1])
# 初始化当前时间和已完成的任务数
current_time = 0
completed = 0
for i in range(len(A)):
# 如果当前时间加上任务需要的时间小于等于deadlinei,则可以完成该任务
if current_time + A[i][0] <= A[i][1]:
current_time += A[i][0]
completed += 1
return completed
# 测试代码
A = [[2, 4], [3, 5], [1, 2], [4, 7], [5, 9]]
print(max_assignments(A)) # 输出3
相关问题
用python按以下要求写出代码,Suppose there are n assignments A= [a1, a2 …ai …an]that you need to complete before the deadline. An assignment ai= [durationi, deadlinei] need durationi days to complete and must be completed before or on deadlinei. You can only do one assignment at a time and start next assignment once the current assignment is completed. Assuming you start on day 1, implement an efficient algorithm to find the maximum number of assignments you can complete.
可以回答这个问题。以下是Python代码:
def max_assignments(A):
A = sorted(A, key=lambda x: x[1])
current_day = 1
count = 0
for duration, deadline in A:
if current_day + duration - 1 <= deadline:
count += 1
current_day += duration
return count
# 测试
A = [[2, 4], [3, 5], [1, 2], [4, 7], [1, 1]]
print(max_assignments(A)) # 输出3,最多可以完成3个作业:[1, 2], [2, 4], [1, 1]
Suppose you are a cyptanalyst and you have intercepted a ciphertext c = 4 that has been sent to Alice. You find Alice’s RSA public key in a key directory: (e, n) = (5, 247). You assume this key has been used to produce the ciphertext. Find Alice’s private key. Show all your working.
To find Alice's private key, we need to use the RSA encryption and decryption formula:
Encryption: c = m^e mod n
Decryption: m = c^d mod n
We know the ciphertext c = 4 and the public key (e, n) = (5, 247). We need to find the private key d.
To find d, we need to use the following formula:
d = e^-1 mod φ(n)
where φ(n) = (p-1)(q-1) is the Euler's totient function of n and p and q are prime factors of n.
To find p and q, we need to factorize n. Since n is relatively small, we can do this manually:
n = 247 = 13 * 19
Now we can calculate φ(n):
φ(n) = (p-1)(q-1) = (13-1)(19-1) = 12 * 18 = 216
To find the inverse of e modulo φ(n), we can use the extended Euclidean algorithm:
```
φ(n) = 216
e = 5
216 = 5 * 43 + 1
1 = 216 - 5 * 43
1 = 216 - 5 * (φ(n) - e * q)
1 = 5 * e * (-43) + φ(n) * 1
Therefore, d = -43 mod 216 = 173
```
Now we can use the decryption formula to find the plaintext m:
m = c^d mod n = 4^173 mod 247 = 25
Therefore, Alice's private key is d = 173 and the plaintext message is m = 25.
阅读全文