대부분의 팁 계산기 앱은 단순히 식사 가격의 일정 비율을 차지합니다. 예를 들어 식사가 $ 23.45 인 경우 15 % 팁 = $ 3.52 또는보다 관대 한 20 % 팁 = $ 4.69를 남길 수 있습니다.
신용 카드 사용자에게 충분히 편리합니다. 그러나 현금 팁을 남기기를 선호한다면 홀수 센트 금액이 방해가됩니다. 따라서 현금 사용자에게 더 편리하도록 아이디어를 수정합시다.
당신의 임무
입력으로 사용 되는 프로그램 또는 함수 를 가능한 한 적은 바이트로 작성하십시오 .
- 식사 가격
- 최소 팁 비율
- 최대 팁 비율
그리고 필요한 청구서 / 은행권 및 동전의 수를 최소화하는 [price * min_percentage / 100, price * max_percentage / 100] 범위 내의 모든 팁 금액을 출력하십시오.
미국 화폐 단위가 1 ¢, 5 ¢, 10 ¢, 25 ¢, $ 1, $ 5, $ 10, $ 20, $ 50 및 $ 100이라고 가정합니다.
예
다음은 파이썬에서 골프가 아닌 예제 프로그램입니다.
import math
import sys
# Do the math in cents so we can use integer arithmetic
DENOMINATIONS = [10000, 5000, 2000, 1000, 500, 100, 25, 10, 5, 1]
def count_bills_and_coins(amount_cents):
# Use the Greedy method, which works on this set of denominations.
result = 0
for denomination in DENOMINATIONS:
num_coins, amount_cents = divmod(amount_cents, denomination)
result += num_coins
return result
def optimize_tip(meal_price, min_tip_percent, max_tip_percent):
min_tip_cents = int(math.ceil(meal_price * min_tip_percent))
max_tip_cents = int(math.floor(meal_price * max_tip_percent))
best_tip_cents = None
best_coins = float('inf')
for tip_cents in range(min_tip_cents, max_tip_cents + 1):
num_coins = count_bills_and_coins(tip_cents)
if num_coins < best_coins:
best_tip_cents = tip_cents
best_coins = num_coins
return best_tip_cents / 100.0
# Get inputs from command-line
meal_price = float(sys.argv[1])
min_tip_percent = float(sys.argv[2])
max_tip_percent = float(sys.argv[3])
print('{:.2f}'.format(optimize_tip(meal_price, min_tip_percent, max_tip_percent)))
일부 샘플 입력 및 출력 :
~$ python tipcalc.py 23.45 15 20
4.00
~$ python tipcalc.py 23.45 15 17
3.55
~$ python tipcalc.py 59.99 15 25
10.00
~$ python tipcalc.py 8.00 13 20
1.05
a program that takes as input (stdin, command-line arguments, or GUI input box, whichever is most convenient in your language)이것은 입력 및 출력에 대한 기본값을 무시하기위한 것입니까? 즉, 세 개의 숫자를 받아 결과를 반환하는 함수가 허용됩니까?
3.51과 3.75또한 테스트 케이스에 유효한 출력입니다 23.45 15 17? 그들은 같은 양의 동전을 사용하고 또한 범위 안에 있습니다.