Algorithm Analysis
Let's consider a solution using brute force, utilizing the conditional operator. The median among , , will be:
, if or ;
, if or ;
, if or .
Let's consider a solution using minimum and maximum functions. The median among , , will be .
Let's consider a solution using sorting. We read three numbers into an array of length 3 and sort it. The middle element of the array is the median among the three numbers.
Algorithm Implementation
We read the input data. Compute the answer using the formula and output it.
scanf("%d %d %d", &a, &b, &c); if ((b <= a && a <= c) || (b >= a && a >= c)) res = a; else if ((a <= b && b <= c) || (a >= b && b >= c)) res = b; else res = c; printf("%d\n", res);
Algorithm Implementation – Using min and max
#include <stdio.h> int a, b, c, res; int min(int x, int y) { return (x < y) ? x : y; } int max(int x, int y) { return (x > y) ? x : y; } int main(void) { scanf("%d %d %d", &a, &b, &c); res = a + b + c - min(a, min(b, c)) - max(a, max(b, c)); printf("%d\n", res); return 0; }
Algorithm Implementation – Using Sorting
#include <cstdio> #include <algorithm> using namespace std; int m[3]; int main(void) { scanf("%d %d %d", &m[0], &m[1], &m[2]); sort(m, m+3); printf("%d\n", m[1]); return 0; }
Java Implementation
import java.util.*; public class Main { static int min(int x, int y, int z) { return Math.min(x, Math.min(y, z)); } static int max(int x, int y, int z) { return Math.max(x, Math.max(y, z)); } public static void main(String[] args) { Scanner con = new Scanner(System.in); int a = con.nextInt(); int b = con.nextInt(); int c = con.nextInt(); int res = a + b + c - max(a, b, c) - min(a, b, c); System.out.println(res); con.close(); } }
Python Implementation
a, b, c = map(int, input().split()) res = a + b + c - min(a, b, c) - max(a, b, c) print(res)