Experiment 3-3 compare size (10 points)

This question requires the input of any 3 integers to be output from small to large.

Input format:

Enter 3 integers in one line, separated by spaces.

Output format:

Output 3 integers from small to large in one line, and connect them with "->".

Input sample:

4 2 8

Sample output:

2->4->8

Code:

# include <stdio.h>
# include <stdlib.h>

int main() {
    
    
    int a,b,c,temp,min;
    scanf("%d %d %d",&a,&b,&c);
    // temp取a和b的最大值
    temp = (a >= b)?a:b;
    min = (a < b)?a:b;
    a = temp;
    b = min;
    if (b >= c) {
    
    
        printf("%d->%d->%d",c,b,a);
    }else {
    
    
        if (a >= c) {
    
    
            printf("%d->%d->%d",b,c,a);
        }else {
    
    
            printf("%d->%d->%d",b,a,c);
        }
    }
}

Submit screenshot:

Insert picture description here

Problem-solving ideas:

There are many ideas for solving problems here. I use two variables to store the maximum and minimum values ​​of a and b respectively, and then force a to be the larger number, b to be the smaller number, and then take the smaller one later. Just compare the number with c!
Note: The situation where two numbers are equal must be considered clearly

Guess you like

Origin blog.csdn.net/weixin_43862765/article/details/114436408