ปานกลาง 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

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

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

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

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