gpt4 book ai didi

python - CodeChef 实践挑战 - "ATM"

转载 作者:太空宇宙 更新时间:2023-11-03 20:25:45 26 4
gpt4 key购买 nike

Pooja would like to withdraw X $US from an ATM. The cash machine will only accept the transaction if X is a multiple of 5, and Pooja's account balance has enough cash to perform the withdrawal transaction (including bank charges). For each successful withdrawal the bank charges 0.50 $US. Calculate Pooja's account balance after an attempted transaction.

Input

Positive integer 0 < X <= 2000 - the amount of cash which Pooja wishes to withdraw.

Nonnegative number 0<= Y <= 2000 with two digits of precision - Pooja's initial account balance.

Output

Output the account balance after the attempted transaction, given as a number with two digits of precision. If there is not enough money in the account to complete the transaction, output the current bank balance.

Example - Successful Transaction

Input: 30 120.00

Output: 89.50

Example - Incorrect Withdrawal Amount (not multiple of 5)

Input: 42 120.00

Output: 120.00

Example - Insufficient Funds

Input: 300 120.00

Output: 120.00

我使用Python3.x来解决这个程序,我的代码是这样的:-

withdrawal_amount, balance_amount = input().split()              
withdrawal_amount = int(withdrawal_amount)
balance_amount = float(balance_amount)
if withdrawal_amount % 5 == 0:
balance_amount = balance_amount - withdrawal_amount - 0.5
print('%.2f' % balance_amount)
else:
print('%.2f' % balance_amount)

它执行得很好,但是当我提交答案时,结果不正确。谁能帮助我并告诉我哪里错了。

问题链接:https://www.codechef.com/problems/HS08TEST

最佳答案

您在这里缺少一个条件,即 if withdrawal_amount >balance_amount。将您的代码修改为此并重试

withdrawal_amount, balance_amount = input().split()              
withdrawal_amount = int(withdrawal_amount)
balance_amount = float(balance_amount)
if (withdrawal_amount % 5 == 0 and balance_amount>(withdrawal_amount+.5)):
balance_amount = balance_amount - withdrawal_amount - 0.5
print('%.2f' % balance_amount)
else:
print('%.2f' % balance_amount)

关于python - CodeChef 实践挑战 - "ATM",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57832647/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com