[C language] Input 3 numbers and assign them to variables a, b, and b, and output them in ascending order

code show as below

//输入3个数赋值给变量a、b、b,要求按由小到大的顺序输出它们
#include <stdio.h>
int main()
{
    
    
	int a,b,c,t;
	printf("Please enter a、b、c:");
	scanf("%d%d%d",&a,&b,&c);
	if(a>b){
    
     //(1)若a>b,则交换a与b位置: b<a c 
		t=a;  //其余情况亦是同理 
		a=b;
		b=t;
	}
	if(a>c){
    
    //(1)若a>c则交换a与c位置:b c<a
		t=a; 
		a=c;
		c=t;
	}
	if(b>c){
    
    //(1)若b>c,则交换b与c位置 即 c<b<a 
		t=b;
		b=c;
		c=t;
	}
	putchar('\n');
	printf("从小到大即:%d,%d,%d",a,b,c);
	printf("\n");

	return 0;	
 } 

The idea used is to classify and discuss, depending on their size, if the former is larger than the latter, then exchange the positions of the two, then after 3 if judgments, the order from small to large can be obtained.

Test output

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/qq_44731019/article/details/123627133