Problema
A rectangle has two sides of length width and two sides of length height. Its perimeter is the total length of all four sides.
Implement rectangle_perimeter(width, height).
Input
width: The width of the rectangle. height: The height of the rectangle.
Output
Return the perimeter as an integer.
Vincoli
1 ≤ width, height ≤ 1,000,000
Esempi
Input:
width = 3
height = 5
Output atteso:
expected = 16
Input:
long long width = 3;
long long height = 5;
Output atteso:
long long expected = 16;
Input:
long width = 3;
long height = 5;
Output atteso:
long expected = 16;
Spiegazione
Two sides of 3 and two sides of 5: 3 + 3 + 5 + 5 = 16.
Input:
width = 7
height = 7
Output atteso:
expected = 28
Input:
long long width = 7;
long long height = 7;
Output atteso:
long long expected = 28;
Input:
long width = 7;
long height = 7;
Output atteso:
long expected = 28;
Spiegazione
A square is a rectangle too: 4 × 7 = 28.
Come affrontarlo
Prova prima da solo, poi apri un suggerimento alla volta.
Suggerimento 1
Un rettangolo ha due larghezze uguali e due altezze uguali.
Suggerimento 2
Somma tutti e quattro i lati, oppure combina prima le due lunghezze ripetute.
Suggerimento 3
Fai attenzione a usare solo aritmetica intera; qui non servono casi speciali.
Complessità attesa
tempo O(1), spazio aggiuntivo O(1)
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.