中級 0件解決済み 正答率0%

Weekly Payroll

Total a week's pay for salaried, hourly and commission employees, each paid by its own rule.

このチャレンジで学べること

  • Compute pay with a rule per employee type
  • Pay overtime above 40 hours
  • Sum decimal amounts accurately

問題

翻訳を表示

A payroll system treats every employee as an Employee with a WeeklyPay() method, and each kind of employee overrides it.

Each employee is written as:

  • salaried <annual>: earns annual / 52.
  • hourly <rate> <hours>: earns rate × hours for the first 40 hours and 1.5 × rate for every hour beyond 40.
  • commission <base> <rate> <sales>: earns base + rate × sales.

Implement weekly_payroll(employees).

Input

employees: The employees. Numbers may have decimals.

Output

Return the total weekly pay. Answers within 1e-6 are accepted.

制約

0 ≤ number of employees ≤ 10,000

All numbers are between 0 and 10^7; hours ≤ 100

例

例 1
入力:
employees = ["salaried 52000", "hourly 20 45"]

期待される出力:
expected = 1950.0
入力:
std::vector<std::string> employees = {"salaried 52000", "hourly 20 45"};

期待される出力:
double expected = 1950.0;
入力:
string[] employees = new string[] {"salaried 52000", "hourly 20 45"};

期待される出力:
double expected = 1950.0;

解説

The salaried employee earns 52000 / 52 = 1000. The hourly one earns 20 × 40 + 30 × 5 = 950. Total 1950.

例 2
入力:
employees = ["commission 300 0.1 2500"]

期待される出力:
expected = 550.0
入力:
std::vector<std::string> employees = {"commission 300 0.1 2500"};

期待される出力:
double expected = 550.0;
入力:
string[] employees = new string[] {"commission 300 0.1 2500"};

期待される出力:
double expected = 550.0;

解説

300 + 0.1 × 2500 = 550.

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

Handle each employee type with its own formula.

ヒント 2

Hourly pay is normal for the first 40 hours and 1.5 times the rate after that.

ヒント 3

Keep decimals: salaries divided by 52 rarely give whole numbers.

目安の計算量

O(number of employees) time, O(1) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。