Simple Sort
Adapted by Neilor Tonin, URI Brazil
Timelimit: 1
Read three integers and sort them in ascending order. After, print these values in ascending order, a blank line and then the values in the sequence as they were readed.
Input
The input contains three integer numbers.
Output
Present the output as requested above.
Input Sample | Output Sample |
7 21 -14 | -14 7 21 7 21 -14 |
-14 21 7 | -14 7 21 -14 21 7 |
Solution
- [tab]
- C
#include <stdio.h> void printInOrder(int a, int b, int c) { int min, mid, max; if(a < b && a < c){ min = a; if(b < c){ mid = b; max = c; }else{ mid = c; max = b; } }else if(b < a && b < c){ min = b; if(a < c){ mid = a; max = c; }else{ mid = c; max = a; } }else{ min = c; if(a < b){ mid = a; max = b; }else{ mid = b; max = a; } } printf("%d\n%d\n%d\n", min, mid, max); } int main() { int a, b, c; scanf("%d %d %d", &a, &b, &c); printInOrder(a, b, c); printf("\n%d\n%d\n%d\n", a, b, c); return 0; }
- C++
#include <iostream> using namespace std; void printInOrder(int a, int b, int c) { int min, mid, max; if(a < b && a < c){ min = a; if(b < c){ mid = b; max = c; }else{ mid = c; max = b; } }else if(b < a && b < c){ min = b; if(a < c){ mid = a; max = c; }else{ mid = c; max = a; } }else{ min = c; if(a < b){ mid = a; max = b; }else{ mid = b; max = a; } } cout << min << endl << mid << endl << max << endl << endl; } int main() { int a, b, c; cin >> a >> b >> c; printInOrder(a, b, c); cout << a << endl << b << endl << c << endl; return 0; }