ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Format an Invoice

Print invoice lines and a total with amounts stored in cents, formatted as dollars and cents.

สิ่งที่คุณจะได้เรียนรู้

  • Store money as whole cents
  • Format cents as dollars with two digits
  • Build report lines and a total

โจทย์

ดูฉบับแปล

Money is usually stored as whole cents to avoid rounding errors, and only formatted as dollars when it is shown.

For each item, write the line <name> x<quantity> = <amount>, where amount = quantity × price in cents. After the items, write Total = <amount> with the sum of all lines.

Format every amount as dollars, a dot and exactly two digits of cents, for example 5 → 0.05 and 1250 → 12.50.

Implement format_invoice(names, quantities, cents).

Input

names, quantities, cents: Matching lists describing each item.

Output

Return the item lines followed by the total line.

เงื่อนไข

0 ≤ number of items ≤ 1,000

1 ≤ quantity ≤ 1,000; 0 ≤ cents ≤ 10^6

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
cents = [250, 375]
names = ["Tea", "Cake"]
quantities = [2, 1]

ผลลัพธ์ที่คาดหวัง:
expected = ["Tea x2 = 5.00", "Cake x1 = 3.75", "Total = 8.75"]
อินพุต:
std::vector<long long> cents = {250, 375};
std::vector<std::string> names = {"Tea", "Cake"};
std::vector<long long> quantities = {2, 1};

ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"Tea x2 = 5.00", "Cake x1 = 3.75", "Total = 8.75"};
อินพุต:
long[] cents = new long[] {250, 375};
string[] names = new string[] {"Tea", "Cake"};
long[] quantities = new long[] {2, 1};

ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"Tea x2 = 5.00", "Cake x1 = 3.75", "Total = 8.75"};

คำอธิบาย

2 × 250 = 500 cents is 5.00 and 375 cents is 3.75; the total is 875 cents: ["Tea x2 = 5.00", "Cake x1 = 3.75", "Total = 8.75"].

ตัวอย่างที่ 2
อินพุต:
cents = [5]
names = ["Gum"]
quantities = [3]

ผลลัพธ์ที่คาดหวัง:
expected = ["Gum x3 = 0.15", "Total = 0.15"]
อินพุต:
std::vector<long long> cents = {5};
std::vector<std::string> names = {"Gum"};
std::vector<long long> quantities = {3};

ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"Gum x3 = 0.15", "Total = 0.15"};
อินพุต:
long[] cents = new long[] {5};
string[] names = new string[] {"Gum"};
long[] quantities = new long[] {3};

ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"Gum x3 = 0.15", "Total = 0.15"};

คำอธิบาย

3 × 5 = 15 cents, which is written 0.15: ["Gum x3 = 0.15", "Total = 0.15"].

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Do all arithmetic in whole cents.

คำใบ้ 2

Split cents into dollars (cents / 100) and the remainder (cents % 100).

คำใบ้ 3

Always print the cents with two digits, so 5 cents is 0.05.

ความซับซ้อนที่คาดหวัง

O(number of items) time, O(number of items) extra space

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

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

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

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