Editorial
Algorithm Analysis
Compare two numbers and output first the smaller, then the larger one.
The second solution variant – check if , then swap their values using a third variable. Then output the numbers and .
Algorithm Implementation
Read the input data.
scanf("%d %d",&a,&b);
Compare and output the numbers in the required order.
if (a < b) printf("%d %d\n",a,b); else printf("%d %d\n",b,a);
Algorithm Implementation – swap
Read the input data.
scanf("%d %d",&a,&b);
If , then swap the values of and .
if (a > b) { temp = a; a = b; b = temp; }
Output the values of and , where .
printf("%d %d\n",a,b);
Algorithm Implementation – ternary operator
Read the input data.
scanf("%d %d",&a,&b);
Calculate the minimum and maximum values among the two numbers and .
min = (a < b) ? a : b; max = (a > b) ? a : b;
Output the smallest and largest among the two numbers and .
printf("%d %d\n",min,max);
Algorithm Implementation – using functions
#include <stdio.h> int a, b; int min(int a, int b) { return (a < b) ? a : b; } int max(int a, int b) { return (a > b) ? a : b; } int main(void) { scanf("%d %d",&a,&b); printf("%d %d\n",min(a,b),max(a,b)); return 0; }
Java Implementation
import java.util.*; public class Main { public static void main(String[] args) { Scanner con = new Scanner(System.in); int a = con.nextInt(); int b = con.nextInt(); System.out.println(Math.min(a,b) + " " + Math.max(a,b)); con.close(); } }
Python Implementation
a, b = map(int, input().split()) if a > b: a, b = b, a print(a, b)
C# Implementation
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleAppCSharp { class Program { static void Main(string[] args) { string[] values = Console.ReadLine().Split(' '); int x = int.Parse(values[0]); int y = int.Parse(values[1]); int min = (x < y) ? x : y; int max = (x > y) ? x : y; Console.WriteLine("{0} {1}", min, max); } } }