Problema
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.
Vincoli
0 ≤ age ≤ 120
Esempi
Input:
age = 10
Output atteso:
expected = 5
Input:
long long age = 10;
Output atteso:
long long expected = 5;
Input:
long age = 10;
Output atteso:
long expected = 5;
Spiegazione
10 is between 3 and 12, so the price is 5.
Input:
age = 70
Output atteso:
expected = 7
Input:
long long age = 70;
Output atteso:
long long expected = 7;
Input:
long age = 70;
Output atteso:
long expected = 7;
Spiegazione
70 is 65 or older, so the senior price of 7 applies.
Come affrontarlo
Prova prima da solo, poi apri un suggerimento alla volta.
Suggerimento 1
The price depends on which age band the visitor falls into.
Suggerimento 2
Check the youngest and oldest special cases before the middle ranges.
Suggerimento 3
Pay attention to the exact endpoints so ages 3, 13, and 65 get the correct prices.
Complessità attesa
O(1) time, O(1) extra space
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.