Problema
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
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.
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
Somma prima i tre valori, poi dividi il totale in tre parti uguali.
Suggerimento 2
Usa la divisione aritmetica normale così i risultati non interi restano visibili.
Suggerimento 3
Fai attenzione ai linguaggi che usano di default la divisione intera, perché perderebbe la parte frazionaria.
Complessità attesa
tempo O(1), spazio aggiuntivo O(1)
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.