Đề bài
A cinema charges by age: children under 3 are free, ages 3–12 pay 5, ages 13–64 pay 12, and 65 or older pay 7.
Implement ticket_price(age).
Input
age: The visitor's age in years.
Output
Return the ticket price.
Ràng buộc
0 ≤ age ≤ 120
Ví dụ
Đầu vào:
age = 10
Kết quả mong đợi:
expected = 5
Đầu vào:
long long age = 10;
Kết quả mong đợi:
long long expected = 5;
Đầu vào:
long age = 10;
Kết quả mong đợi:
long expected = 5;
Giải thích
10 is between 3 and 12, so the price is 5.
Đầu vào:
age = 70
Kết quả mong đợi:
expected = 7
Đầu vào:
long long age = 70;
Kết quả mong đợi:
long long expected = 7;
Đầu vào:
long age = 70;
Kết quả mong đợi:
long expected = 7;
Giải thích
70 is 65 or older, so the senior price of 7 applies.
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
The price depends on which age band the visitor falls into.
Gợi ý 2
Check the youngest and oldest special cases before the middle ranges.
Gợi ý 3
Pay attention to the exact endpoints so ages 3, 13, and 65 get the correct prices.
Độ phức tạp mong đợi
O(1) time, O(1) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.