Tutte le sfide
Facile 12 risolte 48.0% di accettazione

Average of Three

Average three whole numbers.

Cosa imparerai

  • Add numbers and divide by their count
  • Avoid integer division surprises
  • Return a decimal result

Problema

Mostra traduzione

Compute the average (arithmetic mean) of three integers. The result can have a fractional part.

Implement average_of_three(a, b, c).

Input

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

Output

Return (a + b + c) / 3 as a decimal number.

Vincoli

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

Esempi

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

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

Output atteso:
double expected = 5.0;
Input:
long a = 2;
long b = 4;
long c = 9;

Output atteso:
double expected = 5.0;

Spiegazione

2 + 4 + 9 = 15 and 15 / 3 = 5.

Esempio 2
Input:
a = 1
b = 2
c = 2

Output atteso:
expected = 1.666667
Input:
long long a = 1;
long long b = 2;
long long c = 2;

Output atteso:
double expected = 1.666667;
Input:
long a = 1;
long b = 2;
long c = 2;

Output atteso:
double expected = 1.666667;

Spiegazione

1 + 2 + 2 = 5 and 5 / 3 ≈ 1.666667. Divide as decimals, not whole numbers.

Come affrontarlo

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

Suggerimento 1

Add the three values first, then spread the total across three equal parts.

Suggerimento 2

Use ordinary arithmetic division so non-integer results are preserved.

Suggerimento 3

Watch for languages that do integer division by default, since that would drop the fractional part.

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 LMLaura Mahlow Python 798 ms - 5/5 Vedi il codice
2 HJhettory Jurash Python 807 ms - 5/5 Vedi il codice
3 RCRiccardo Corti Python 838 ms - 5/5 Vedi il codice
4 ゼゼロダニ Python 849 ms - 5/5 Vedi il codice
5 KKKaneki Ken Python 858 ms - 5/5 Vedi il codice
6 ДЗДавид Зайдулин Python 992 ms - 4/5 Vedi il codice
7 PKPornchai Kaewkrabil Go 1,195 ms - 4/5 Vedi il codice
8 GTGarlic The Destroyer Rust 1,403 ms - 4/5 Vedi il codice
9 ВЛВладимир Ленин C++ 3,127 ms - 4/5 Vedi il codice
10 PTphúc thịnh phạm C++ 3,183 ms - 4/5 Vedi il codice

Impara dagli altri

Soluzioni della community

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