Write code to output the three numbers in order

1. Ideas
First compare two pairs to determine the maximum value, and then determine the next maximum value and minimum value. For example, to compare the output of a, b, and c in order, first compare the sizes of a and b, and compare the larger value of a and b with c. The larger value obtained is the largest of the three numbers. Value, compare the smaller value of a and b with c, the larger value obtained is the second largest value among the three numbers, and the smaller value obtained is the smallest value of the three numbers.
2. Key points
When obtaining the maximum and minimum values ​​of two numbers, an intermediate amount is needed to act as a buffer to temporarily store the data. For example, when comparing the size of a and b, if a<b, you must use the intermediate quantity temp to first assign the value of a to temp, then assign the value of b to a, and then assign the data in temp to b, so As a result, the values ​​of a and b are successfully exchanged. The value of a is the value of the larger value of a and b, and the value of b is the value of the smaller value of a and b.
3.
Press the three numbers a, b, c from large to small to output the code as follows:

#include<stdio.h>
int main()
{
    
     
 int a, b,c,temp;
 printf("请输入a的值:a="); 
 scanf("%d", &a);  
 printf("请输入b的值:b=");  
 scanf("%d", &b);
 printf("请输入c的值:c="); 
 scanf("%d", &c);
 if (a < b)
   {
    
    
     temp = a;
      a = b;
      b = temp;
   } 
  if (a < c)
   {
    
     
      temp = a ;  
      a = c;  
      c = temp; 
   } 
  if 
      (b < c)
       {
    
      
         temp = b ;  
         b = c;  
         c = temp; 
       } 
   printf("%d %d %d\n", a, b, c);  
   return 0;
  }

Guess you like

Origin blog.csdn.net/XKA_HRX/article/details/109135896