Tutte le sfide
Facile 27 risolte 49.1% di accettazione

Rectangle Perimeter

Compute the perimeter of a rectangle from its sides.

Cosa imparerai

  • Store values in variables
  • Apply a simple formula
  • Return a computed result

Problema

Mostra traduzione

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

A rectangle has two equal widths and two equal heights.

Suggerimento 2

Add all four sides, or combine the two repeated side lengths first.

Suggerimento 3

Watch for integer arithmetic only; no special cases are needed here.

Complessità attesa

O(1) time, O(1) extra space

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 ゼゼロダニ Python 782 ms - 4/5 Vedi il codice
2 TATiến Anh Nguyễn Python 805 ms - 5/5 Vedi il codice
3 DDdang duong Python 818 ms - 5/5 Vedi il codice
4 HJhettory Jurash Python 820 ms - 5/5 Vedi il codice
5 JVJulien VACHAUDEZ Python 827 ms - 5/5 Vedi il codice
6 LMliam morales Python 831 ms - 5/5 Vedi il codice
7 ДЗДавид Зайдулин Python 834 ms - 4/5 Vedi il codice
8 RCRiccardo Corti Python 835 ms - 5/5 Vedi il codice
9 Nnongatomeiei Python 838 ms - 4/5 Vedi il codice
10 LMLaura Mahlow Python 845 ms - 5/5 Vedi il codice
11 JSJithin Sebastian Python 851 ms - 5/5 Vedi il codice
12 Ccheodo Python 857 ms - 5/5 Vedi il codice
13 KKKaneki Ken Python 862 ms - 5/5 Vedi il codice
14 Н"Никита "Programmer" Python 865 ms - 5/5 Vedi il codice
15 DBDaltrey Barbosa Python 871 ms - 4/5 Vedi il codice
16 MCMathieu CROS Python 880 ms - 5/5 Vedi il codice
17 PKPornchai Kaewkrabil Go 1,154 ms - 5/5 Vedi il codice
18 GTGarlic The Destroyer Rust 1,981 ms - 5/5 Vedi il codice
19 โสโสรัจจา สังข์แก้ว C++ 3,072 ms - 5/5 Vedi il codice
20 NNNhân Nguyễn Thiện C++ 3,082 ms - 4/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 26 soluzioni complete sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le revisioni dell'IA. Accedi e risolvi la sfida per partecipare alla discussione.