Three integers a, b, c are given on one line separated by spaces. Print the one that lands in the middle when the three are put in order of size. Equal values are allowed (e.g. for 5 5 1 the middle is 5). Try solving it with comparisons only, without a sort function or sorting a list.
Input
-1,000,000,000 ≤ a, b, c ≤ 1,000,000,000
Output
the middle number
Example 1 · stdin
3 9 5
stdout
5
Example 2 · stdin
5 5 1
stdout
5
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
Equal values are the trap. Using only <, as in a < b < c or c < b < a, lets 5 5 1 slip through every branch into the final else. Splitting on a >= b first and then asking where c goes (nested conditions) covers equal values too. Chained comparison a < b < c reads like math only in Python. In C, C++, Java, C# and JS you write a < b && b < c (C, C++ and JS accept a < b < c, but it compares the truth value of (a < b) — 1/0 or true/false — with c). In JS/TS, forgetting Number makes it a string comparison ("10" < "9" is true). Craykon nests the same way: if (a >= b) { if (…) { } else { } }. A variable first created inside a branch is not visible outside it (same in Java, C#, C, C++), so create mid before the branches; Python differs — a variable made inside an if is visible afterwards.
Pythonmain.py · 16 lines
a, b, c = map(int, input().split())if a >= b: if b >= c: mid = b # a >= b >= c elif a >= c: mid = c # a >= c > b else: mid = a # c > a >= belse: if a >= c: mid = a # b > a >= c elif b >= c: mid = c # b >= c > a else: mid = b # c > b > aprint(mid)
JavaScriptmain.js · 12 lines
const [a, b, c] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let mid;if (a >= b) { if (b >= c) mid = b; else if (a >= c) mid = c; else mid = a;} else { if (a >= c) mid = a; else if (b >= c) mid = c; else mid = b;}console.log(mid);
TypeScriptmain.ts · 13 lines
import { readFileSync } from "node:fs";const [a, b, c]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let mid: number;if (a >= b) { if (b >= c) mid = b; else if (a >= c) mid = c; else mid = a;} else { if (a >= c) mid = a; else if (b >= c) mid = c; else mid = b;}console.log(mid);
JavaMain.java · 19 lines
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(); int mid; if (a >= b) { if (b >= c) mid = b; else if (a >= c) mid = c; else mid = a; } else { if (a >= c) mid = a; else if (b >= c) mid = c; else mid = b; } System.out.println(mid); }}
C#Main.cs · 24 lines
using System;class Program{ static void Main() { int[] v = Array.ConvertAll(Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries), int.Parse); int a = v[0], b = v[1], c = v[2]; int mid; if (a >= b) { if (b >= c) mid = b; else if (a >= c) mid = c; else mid = a; } else { if (a >= c) mid = a; else if (b >= c) mid = c; else mid = b; } Console.WriteLine(mid); }}
C++main.cpp · 16 lines
#include <iostream>int main() { int a, b, c, mid; std::cin >> a >> b >> c; if (a >= b) { if (b >= c) mid = b; else if (a >= c) mid = c; else mid = a; } else { if (a >= c) mid = a; else if (b >= c) mid = c; else mid = b; } std::cout << mid << "\n";}
Cmain.c · 17 lines
#include <stdio.h>int main(void) { int a, b, c, mid; scanf("%d %d %d", &a, &b, &c); if (a >= b) { if (b >= c) mid = b; else if (a >= c) mid = c; else mid = a; } else { if (a >= c) mid = a; else if (b >= c) mid = c; else mid = b; } printf("%d\n", mid); return 0;}
Craykonmain.cray · 36 lines
v = Console.ReadNumbers();a = v[0];b = v[1];c = v[2];mid = 0;if (a >= b){ if (b >= c) { mid = b; } else if (a >= c) { mid = c; } else { mid = a; }}else{ if (a >= c) { mid = a; } else if (b >= c) { mid = c; } else { mid = b; }}Console.WriteLine(mid);