Title: Input 3 integers, from small to large sorted after output
Sample input:
20 7 33
Sample output:
7 20 33
First, declare three integers a,b,c and a temporary variable t:
int a,b,c,t; // 1,b,c is three integers and T is a temporary variable
Enter a three integer:
scanf ("%d%d%d", &a,&b,&c);
Make 3 if judgments:
1. If b>a, A/b is swapped
2. If c>a, then a,c swap
3. If c>b, then b,c swap
Code:
if (b>=a) { T=B; b=A; A=t;} if (c>=a) { T=c;c=a;a=t;} if (c>=b) { T=c;c=b;b=t;}
When sorting is complete, output 3 numbers in order from large to small:
printf ("%d%d%d", a,b,c);
Full code:
//P11 Example 1-5 ordering of three integers#include <cstdio>intA,b,c,t;//1,b,c is three integers and T is a temporary variableintMain () {scanf ("%d%d%d",&a,&b,&c); if(b>=a) {T=b; b=A; A=T; } if(c>=a) {T=c;c=a;a=T; } if(c>=b) {T=c;c=b;b=T; } printf (" %d%d%d", A,b,c); return 0; }
An introduction to the C + + algorithm Contest Classic Page11 example 1-5 three-integer ordering