Seven problems where the work fits in one small function: write it above main,
call it for every value, and let main do the reading and printing.
- The values in Absolute Value reach 10¹⁸ in size: the parameter and the return
type are
long long. - A number is even when
x % 2 == 0. Don't test for odd withx % 2 == 1: in C++,-3 % 2is-1. - Clamp asks for a function by name. Two early
returns and a last one cover all three cases. - Leap Years: a
bool isLeap(int year)with the rule from the statement, and a counter inmain. - In Count Valid Triangles the sides go up to 10⁹, and the sum of two of them
doesn't fit in an
int. - Reverse a Number: build the reversed number digit by digit,
r = r * 10 + n % 10. The zeros at the end of 1200 disappear by themselves. - Fast Power promises that aᵇ fits in a
long longand that b ≤ 60, so b multiplications in a loop are fast enough. Remember a⁰ = 1, even for a = 0.