Problème
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.
Contraintes
1 ≤ width, height ≤ 1,000,000
Exemples
Entrée :
width = 3
height = 5
Sortie attendue :
expected = 16
Entrée :
long long width = 3;
long long height = 5;
Sortie attendue :
long long expected = 16;
Entrée :
long width = 3;
long height = 5;
Sortie attendue :
long expected = 16;
Explication
Two sides of 3 and two sides of 5: 3 + 3 + 5 + 5 = 16.
Entrée :
width = 7
height = 7
Sortie attendue :
expected = 28
Entrée :
long long width = 7;
long long height = 7;
Sortie attendue :
long long expected = 28;
Entrée :
long width = 7;
long height = 7;
Sortie attendue :
long expected = 28;
Explication
A square is a rectangle too: 4 × 7 = 28.
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
Un rectangle a deux largeurs égales et deux hauteurs égales.
Indice 2
Additionne les quatre côtés, ou combine d’abord les deux longueurs répétées.
Indice 3
Fais attention à n’utiliser que des entiers ; aucun cas particulier n’est nécessaire ici.
Complexité attendue
temps O(1), espace supplémentaire O(1)
Références
Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.