C language three Numbers to arrange the size of the implementation of the method

  • 2020-05-24 05:49:42
  • OfStack

This is a simple problem, and there are a lot of ways to do it, but I want to do it mathematically. ps: I am a student with poor grades. I can't write the program well. Welcome to help me.

Combing thoughts:

Take 1, 2, and 3 as examples and consider all of our possible input types (not listed here) : 1, 2, 3; 3 2 1; 1 2 3; 1 1 1; 1 2 2; 1 2 1; The overall idea is that after entering 3 Numbers, we will take 2 Numbers from them, and select the largest number from the two Numbers and compare the difference between the largest number and the third number. Please refer to the code to understand.

Code:


#include<stdio.h>
int comp(int x,int y)
{
return x>y?x:y;
}
int main()
{
int i,j,k;
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
j=comp(a,b);//j Is the largest value of two Numbers 
k=c;//a+b-j O is a,b The smaller number in; 
if(j-k<0){printf("%d %d %d",k,j,a+b-j);return 0;} // instructions K Maximum output in order 1 2 3 ; 2 2 3
if(j-k>=a+b-j){printf("%d %d %d",j,a+b-j,k);return 0;}//3 2 1
if(j-k<a+b-j){printf("%d %d %d",j,k,a+b-j);return 0;}// 3 2 3
}

Conclusion:

In fact, this code is similar in nature to if statement comparison and sorting, except that instead of calling three if statements at a time, one if statement can output the answer more efficiently by 1 point.


Related articles: