Tutte le sfide
Facile 7 risolte 87.5% di accettazione

Largest of Three

Pick the largest of three numbers without a built-in max.

Cosa imparerai

  • Compare values without a built-in max
  • Combine conditions with and
  • Handle equal values

Problema

Mostra traduzione

Return the largest of three integers. Practice comparisons with if statements; don't use a built-in max function.

Implement largest_of_three(a, b, c).

Input

a: The first number. b: The second number. c: The third number.

Output

Return the largest value.

Vincoli

-1,000,000,000 ≤ a, b, c ≤ 1,000,000,000

Esempi

Esempio 1
Input:
a = 3
b = 9
c = 4

Output atteso:
expected = 9
Input:
long long a = 3;
long long b = 9;
long long c = 4;

Output atteso:
long long expected = 9;
Input:
long a = 3;
long b = 9;
long c = 4;

Output atteso:
long expected = 9;

Spiegazione

9 is greater than both 3 and 4.

Esempio 2
Input:
a = -5
b = -2
c = -2

Output atteso:
expected = -2
Input:
long long a = -5;
long long b = -2;
long long c = -2;

Output atteso:
long long expected = -2;
Input:
long a = -5;
long b = -2;
long c = -2;

Output atteso:
long expected = -2;

Spiegazione

-2 appears twice and is the largest value.

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

Compare the numbers pair by pair to identify the largest one.

Suggerimento 2

A few conditional checks are enough; no search structure is needed.

Suggerimento 3

Be careful with equal values, since ties should still return the correct largest number.

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 799 ms - 3/5 Vedi il codice
2 RCRiccardo Corti Python 847 ms - 5/5 Vedi il codice
3 KKKaneki Ken Python 850 ms - 4/5 Vedi il codice
4 LMLaura Mahlow Python 850 ms - 3/5 Vedi il codice
5 PKPornchai Kaewkrabil Go 1,185 ms - 2/5 Vedi il codice
6 HJhettory Jurash C++ 3,162 ms - 2/5 Vedi il codice
7 PTphúc thịnh phạm C++ 3,269 ms - 4/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 7 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.