Tutte le sfide
Facile 1 risolta 100.0% di accettazione

Rectangle Perimeter

Compute the perimeter of a rectangle from its sides.

Cosa imparerai

  • Salvare valori in variabili
  • Applicare una formula semplice
  • Restituire un risultato calcolato

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

Esempio 1
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.

Esempio 2
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.

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 1 soluzione completa sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le recensioni AI. Accedi e risolvi la sfida per partecipare alla discussione.