Example 66- classic C language input three numbers a, b, c, the output order of size

1 title

Input number 3 a,b,c, the output order of size.

2 analysis

Sort three numbers, you only need to compare three

  1. aAnd bcomparing, if a > bthe exchange aand bthe value of
  2. aAnd ccomparing, if a > cthe exchange aand cthe value of
  3. bAnd ccomparing, if b > cthe exchange band cthe value of

After three comparison, and then output a, b, cis arranged in order

3 Implementation

#include <stdio.h>

int main()
{
    int a;
    int b;
    int c;
    int t; // 临时变量用于交换两变量的值
    printf("请输入a、b、c的值,中间用空格隔开:");
    scanf("%d%d%d", &a, &b, &c);
    if (a > b) {
        t = a;
        a = b;
        b = t;
    }
    if (a > c) {
        t = a; 
        a = c;
        c = t;
    }
    if (b > c) {
        t = b;
        b = c;
        c = t;
    }
    printf("排序后a、b、c的值为%d、%d、%d", a, b, c);
}

4 run results

请输入a、b、c的值,中间用空格隔开:4 1 7
排序后a、b、c的值为147
Published 105 original articles · won praise 56 · views 60000 +

Guess you like

Origin blog.csdn.net/syzdev/article/details/104356187