ง่าย 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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก