比如说我想给一些人一些钱每个员工雇员必须得到$a和$b之间的美元。第一个雇员得到$a,随后的每个雇员得到$k,直到该金额超过$b,在这种情况下,该雇员得到$b,随后的每个雇员得到$k,直到该金额低于$a,在这种情况下,该雇员将得到$a,所有n个雇员的周期继续。我想把全部的奖金退还给所有员工
到目前为止我所拥有的:
#!/bin/python3
import os
import sys
def payEmp(n, a, b, k):
totalPayOut = 0
currentPay = 0
increase = True
for i in range(n):
if increase == True:
if currentPay < a:
currentPay += a
else:
currentPay += k
if currentPay >= b:
totalPayOut += b
increase = False
else:
totalPayOut += currentPay
else:
currentPay -= k
if currentPay <= a:
totalPayOut += a
increase = True
else:
totalPayOut += currentPay
return totalPayOut
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
t = int(input())
for t_itr in range(t):
nabk = input().split()
n = int(nabk[0])
a = int(nabk[1])
b = int(nabk[2])
k = int(nabk[3])
result = payEmp(n, a, b, k)
fptr.write(str(result) + '\n')
fptr.close()
基于Haskell的解决方案,我在这个问题上发表了一个评论,下面是一个使用
itertools
库的解决方案,它使用了一个无限的值列表。你知道吗这是因为我们用这个算法封装的模式是无限循环模式
[range(a, b+1, k), range(b, a-1, -k)]
(遍历k
从a
到b
,然后遍历-k
从b
到a
)。itertools.chain.from_iterable
从单个range
迭代器中提取,并且islice
确保我们只获取n
的结果。你知道吗cyc
和chained
是无穷级数,而islice
正做着限制n
学生的工作。你知道吗我可能会使用生成器函数来创建支出:
输出:
相关问题 更多 >
编程相关推荐