现有i张十元纸币,k张五元纸币,j张两元纸币,购物后要支付n元(i,j,k,n都为整数)
def FindSolution(n):
i = n // 10
if (n - i * 10) % 2 == 1:
if (n % 10 == 1 or n % 10 == 3) and i >= 1:
i -= 1
j = 1
k = (n - i * 10 - j * 5) // 2
else:
j = (n - i * 10) // 5
k = (n - i * 10 - j * 5) // 2
else:
j = 0
k = (n - i * 10) // 2
if (n - i * 10 - j * 5 - k * 2) == 0:
print(f'需要{i}张十元纸币,{j}张5元纸币,{k}张两元纸币,{n}刚好凑满元')
return True
else:
print(f'不能刚好凑满{n}元')
return False
网友评论