모든 챌린지
쉬움 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에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

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

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

다른 사람의 풀이 보기

커뮤니티 솔루션

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