Tutte le sfide
Facile 0 risolte 0% di accettazione

Smallest of Three

Return the smallest of three integers.

Cosa imparerai

  • Keep track of the best value so far
  • Compare values with if statements
  • Handle ties and negative numbers

Problema

Mostra traduzione

Three friends compare how many minutes they were late this week and the one who was late the least gets to pick lunch. Given the three numbers, find the smallest. Some values can be equal, and they can be negative (early).

Implement smallest_of_three(a, b, c).

Input

a: The first number.

b: The second number.

c: The third number.

Output

Return the smallest of the three values. If several are equal and smallest, return that value.

Vincoli

−10^18 ≤ a, b, c ≤ 10^18

Esempi

Esempio 1
Input:
a = 7
b = 15
c = 3

Output atteso:
expected = 3
Input:
long long a = 7;
long long b = 15;
long long c = 3;

Output atteso:
long long expected = 3;
Input:
long a = 7;
long b = 15;
long c = 3;

Output atteso:
long expected = 3;

Spiegazione

3 is less than both 7 and 15.

Esempio 2
Input:
a = -4
b = -4
c = 10

Output atteso:
expected = -4
Input:
long long a = -4;
long long b = -4;
long long c = 10;

Output atteso:
long long expected = -4;
Input:
long a = -4;
long b = -4;
long c = 10;

Output atteso:
long expected = -4;

Spiegazione

The two −4 values tie for smallest, so the answer is −4.

Come affrontarlo

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

Suggerimento 1

Assume the first number is the smallest.

Suggerimento 2

Compare it with the second and then the third, updating when you find a smaller one.

Suggerimento 3

Use a 64-bit integer type, the values can be very large.

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.

Ancora nessuna soluzione superata. Risolvila e prendi il primo posto.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.