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

Grocery List Totals

Add up a grocery receipt in cents, charging sales tax only on taxable items.

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

  • Split a line into fields and convert numbers
  • Keep separate totals for taxable items
  • Round tax to the nearest cent

โจทย์

ดูฉบับแปล

A checkout program prints the bottom of a receipt. Each item is written as name quantity price flag: the name is one word, the quantity is a whole number, the price is the unit price in cents and the flag is T for a taxable item or N for a tax free one (most basic groceries).

The subtotal is the sum of quantity × price over all items. The tax is tax_percent percent of the taxable items' total, rounded to the nearest cent with half a cent rounding up. The total is the subtotal plus the tax.

Implement grocery_totals(items, tax_percent).

Input

items: The receipt lines.

tax_percent: The sales tax rate as a whole percentage.

Output

Return [subtotal, tax, total] in cents. An empty receipt gives [0, 0, 0].

เงื่อนไข

0 ≤ number of items ≤ 1,000

1 ≤ quantity ≤ 100

1 ≤ price ≤ 100,000

0 ≤ tax_percent ≤ 20

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
items = ["milk 2 349 N", "soap 1 450 T", "bread 1 299 N"]
tax_percent = 7

ผลลัพธ์ที่คาดหวัง:
expected = [1447, 32, 1479]
อินพุต:
std::vector<std::string> items = {"milk 2 349 N", "soap 1 450 T", "bread 1 299 N"};
long long tax_percent = 7;

ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {1447, 32, 1479};
อินพุต:
string[] items = new string[] {"milk 2 349 N", "soap 1 450 T", "bread 1 299 N"};
long tax_percent = 7;

ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {1447, 32, 1479};

คำอธิบาย

The subtotal is 698 + 450 + 299 = 1447. Only soap is taxable: 7% of 450 is 31.5, which rounds up to 32, so the total is 1479.

ตัวอย่างที่ 2
อินพุต:
items = ["candy 3 125 T", "towels 2 599 T"]
tax_percent = 6

ผลลัพธ์ที่คาดหวัง:
expected = [1573, 94, 1667]
อินพุต:
std::vector<std::string> items = {"candy 3 125 T", "towels 2 599 T"};
long long tax_percent = 6;

ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {1573, 94, 1667};
อินพุต:
string[] items = new string[] {"candy 3 125 T", "towels 2 599 T"};
long tax_percent = 6;

ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {1573, 94, 1667};

คำอธิบาย

Both items are taxable: 375 + 1198 = 1573, and 6% of 1573 is 94.38, which rounds to 94: [1573, 94, 1667].

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

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

คำใบ้ 1

Split each line on spaces into four fields.

คำใบ้ 2

Add every line to the subtotal, and taxable lines to a second total.

คำใบ้ 3

Tax is taxable × percent / 100 rounded to nearest, computed once at the end.

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

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

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

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

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

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

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

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

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