問題
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.
制約
0 ≤ age ≤ 120
例
例 1
入力:
age = 10
期待される出力:
expected = 5
入力:
long long age = 10;
期待される出力:
long long expected = 5;
入力:
long age = 10;
期待される出力:
long expected = 5;
解説
10 is between 3 and 12, so the price is 5.
例 2
入力:
age = 70
期待される出力:
expected = 7
入力:
long long age = 70;
期待される出力:
long long expected = 7;
入力:
long age = 70;
期待される出力:
long expected = 7;
解説
70 is 65 or older, so the senior price of 7 applies.
解き方のヒント
まずは自分で考えてから、ヒントを1つずつ開いてみましょう。
ヒント 1
The price depends on which age band the visitor falls into.
ヒント 2
Check the youngest and oldest special cases before the middle ranges.
ヒント 3
Pay attention to the exact endpoints so ages 3, 13, and 65 get the correct prices.
目安の計算量
O(1) time, O(1) extra space
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。