Fastest way of finding the middle value of a triple?

前端 未结 25 873
庸人自扰
庸人自扰 2020-12-04 12:20

Given is an array of three numeric values and I\'d like to know the middle value of the three.

The question is, what is the fastest way of finding the midd

相关标签:
25条回答
  • 2020-12-04 12:38

    or a one liner for finding the index in the array containing the middle value:

     int middleIndex = (a[0]<a[1]) ? ((a[0]<a[2) ? a[2] : a[0]) : ((a[1]<a[2) ? a[2] : a[1]);
    
    0 讨论(0)
  • 2020-12-04 12:39

    I didn't see a solution which implements swaps:

    int middle(int a, int b, int c) {
        // effectively sort the values a, b & c
        // putting smallest in a, median in b, largest in c
    
        int t;
    
        if (a > b) {
            // swap a & b
            t = a;
            a = b;
            b = t;
        }
    
        if (b > c) {
            // swap b & c
            t = b;
            b = c;
            c = t;
    
            if (a > b) {
                // swap a & b
                t = a;
                a = b;
                b = t;
            }
        }
    
        // b always contains the median value
        return b;
    }
    
    0 讨论(0)
  • 2020-12-04 12:40

    The easiest way is through sorting. For example consider this code :

    import java.util.Arrays;
    
    
    int[] x = {3,9,2};
    Arrays.sort(x); //this will sort the array in ascending order 
    
    //so now array x will be x = {2,3,9};
    //now our middle value is in the middle of the array.just get the value of index 1
    //Which is the middle index of the array.
    
    int middleValue = x[x.length/2]; // 3/2 = will be 1
    

    That's it.It's that much simple.

    In this way you don't need to consider the size of the array.So if you have like 47 different values then you can also use this code to find the middle value.

    0 讨论(0)
  • 2020-12-04 12:41

    This can be done with two comparisons at most.

    int median(int a, int b, int c) {
        if ( (a - b) * (c - a) >= 0 ) // a >= b and a <= c OR a <= b and a >= c
            return a;
        else if ( (b - a) * (c - b) >= 0 ) // b >= a and b <= c OR b <= a and b >= c
            return b;
        else
            return c;
    }
    
    0 讨论(0)
  • 2020-12-04 12:43

    Method 1

    int a,b,c,result;
    printf("enter three number");
    scanf("%d%d%d",&a,&b,&c);
    result=a>b?(c>a?a:(b>c?b:c)):(c>b?b:(a>c?a:c));
    printf("middle %d",result);
    

    Method 2

    int a=10,b=11,c=12;
    //Checking for a is middle number or not
    if( b>a && a>c || c>a && a>b )
    {
        printf("a is middle number");
    }
    
    //Checking for b is middle number or not
    if( a>b && b>c || c>b && b>a )
    {
        printf("b is middle number");
    }
    
    //Checking for c is middle number or not
    if( a>c && c>b || b>c && c>a )
    {
        printf("c is middle number");
    }
    

    Method 3

    if(a>b)
    {
        if(b>c)
        {
            printf("b is middle one");
        }
        else if(c>a)
        {
            printf("a is middle one");
        }
        else
        {
            printf("c is middle one");
        }
    }
    else
    {
        if(b<c)
        {
            printf("b is middle one");
        }
        else if(c<a)
        {
            printf("a is middle one");
        }
        else
        {
            printf("c is middle one");
        }
    }
    

    I got appropriate ans of finding the middle value of a triple

    0 讨论(0)
  • 2020-12-04 12:45

    And one more idea. There are three numbers {a,b,c}. Then:

    middle = (a + b + c) - min(a,b,c) - max(a,b,c);
    

    Of course, we have to remember about numeric limits...

    0 讨论(0)
提交回复
热议问题