모든 챌린지
쉬움 0명 해결 정답률 0%

Tip Calculator

Work out the tip, the total and each person's share of a restaurant bill, all in cents.

배우게 될 내용

  • Keep money in cents to avoid floating point errors
  • Round to the nearest cent with integers
  • Round a division up

문제

번역본 보기

A group of friends splits a restaurant bill. All amounts are in cents to avoid floating point surprises.

  • The tip is tip_percent percent of the bill, rounded to the nearest cent (exactly half a cent rounds up).
  • The total is the bill plus the tip.
  • Each person pays the total divided by people, rounded up to a whole cent so the group never pays too little.

Implement tip_calculator(bill_cents, tip_percent, people).

Input

bill_cents: The bill before the tip, in cents.

tip_percent: The tip as a whole percentage.

people: How many people share the total.

Output

Return [tip, total, per_person], all in cents.

제약 조건

0 ≤ bill_cents ≤ 10,000,000

0 ≤ tip_percent ≤ 100

1 ≤ people ≤ 50

예시

예시 1
입력:
bill_cents = 4850
tip_percent = 18
people = 3

예상 출력:
expected = [873, 5723, 1908]
입력:
long long bill_cents = 4850;
long long tip_percent = 18;
long long people = 3;

예상 출력:
std::vector<long long> expected = {873, 5723, 1908};
입력:
long bill_cents = 4850;
long tip_percent = 18;
long people = 3;

예상 출력:
long[] expected = new long[] {873, 5723, 1908};

설명

18% of 4850 is 873, the total is 5723, and 5723 / 3 = 1907.67 rounds up to 1908: [873, 5723, 1908].

예시 2
입력:
bill_cents = 1250
tip_percent = 15
people = 2

예상 출력:
expected = [188, 1438, 719]
입력:
long long bill_cents = 1250;
long long tip_percent = 15;
long long people = 2;

예상 출력:
std::vector<long long> expected = {188, 1438, 719};
입력:
long bill_cents = 1250;
long tip_percent = 15;
long people = 2;

예상 출력:
long[] expected = new long[] {188, 1438, 719};

설명

15% of 1250 is 187.5, which rounds up to 188; the total 1438 splits evenly into 719: [188, 1438, 719].

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

bill × percent / 100 is the exact tip; add half of the divisor before dividing to round to nearest.

힌트 2

For rounding a division up, (a + b − 1) / b works with non-negative integers.

힌트 3

Watch for overflow: use 64-bit integers.

예상 복잡도

O(1) time, O(1) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

아직 통과한 풀이가 없습니다. 풀어서 1위를 차지하세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.