Biggest and smallest of four integers (No arrays, no functions, fewest 'if' statements)

后端 未结 18 2420
北海茫月
北海茫月 2020-12-10 11:29

You see, I\'ve self-taught myself C++ (not completely, I\'m still procrastinating -_-). So, now I started university and they\'re teaching C and they made us do a program of

相关标签:
18条回答
  • 2020-12-10 11:59

    The whole point of the classroom problem that requests finding the largest and the smallest simultaneously is to teach you to extract maximum valuable information from each comparison.

    For example, if you know that a > b is true, from that single comparison you should realize that a is no longer candidate for the smallest and should no longer participate in any comparisons dedicated to finding the smallest. And, at the same time, you should realize that b is no longer candidate for the largest. With 4 numbers, two tests a > b and c > d already clearly separate the numbers into two independent classes: two candidates for the largest and two candidates for the smallest. The rest is straightforward.

    In other words, the whole idea is to find the extreme values in parallel, using the information provided by each comparison to further the task of finding both the smallest and the largest value.

    if (first > second) { 
      int t = first; first = second; second = t; 
    }
    
    if (third > fourth) { 
      int t = third; third = fourth; fourth = t; 
    }
    
    /* Now 'first' and 'third' are candidates for the smallest,
       while 'second' and 'fourth' are candidates for the largest */
    
    int min = first < third ? first : third;
    int max = second > fourth ? second : fourth;
    

    As you can see, this requires only four comparisons to find both numbers.

    Note that the above code gives you the values of the smallest and the largest, but it does not tell you the original "index" of the number that provided each value. It is not immediately clear whether it is really necessary. The text of your question says nothing about that, while the code sample you provided implements it. In any case, it is not difficult to update the above code to make it to "track" the origins of the numbers.

    0 讨论(0)
  • 2020-12-10 11:59

    For absolute performance ie. Minimum comparisons and assignments.

    The comments at each level show the candidate values for min and max. The idea is to reduce the sets at each level until there is only one item for each set. This can be done with 4 comparisons and 2 assignments only.

            // min = a b c d
            // max = a b c d
            if (a <= b)
            {
                // min = a c d
                // max = b c d
                if ( c <= d){
                    // min = a c
                    // max = b d
                    min = a <= c ? a : c;
                    max = b > d ? b : d;
                }else{
                    // min = a d
                    // max = b c
                    min = a <= d ? a : d;
                    max = b > c ? b : c;
                }
            }
            else
            {
                // min = b c d
                // max = a c d
                if ( c <= d){
                    // min = b c
                    // max = a d
                    min = b < c ? b : c;
                    max = a > d ? a : d;
                }else{
                    // min = b d
                    // max = a c
                    min = b < d ? b : d;
                    max = a > c ? a : c;
                }
            }
    
    0 讨论(0)
  • 2020-12-10 11:59

    We can make use of condition statements here.

    int max,max1,max2,min,min1,min2;
    max = (max1 = a>b?a:b)>(max2 = c>d?c:d)?max1:max2 ;
    min = (min1 = a<b?a:b)<(min2 = c<d?c:d)?min1:min2 ;
    
    0 讨论(0)
  • 2020-12-10 12:05

    This is too easy, given that the numbers are a,b,c,d:

    #define min(a,b)  ((a) < (b) ? (a) : (b))
    #define max(a,b)  ((a) > (b) ? (a) : (b))
    biggest  = max (max(a,b), max(c,d))
    smallest = min (min(a,b), min(c,d))
    

    Here you go, no if statements, no functions (though the latter is the most stupid and harmful to adepts requirement I ever heard of).

    0 讨论(0)
  • 2020-12-10 12:05

    One idea may be to compute the maximum and minimum of the first two numbers. Then, you compare the rest of the numbers in pairs. The greater one of each pair is compared against the current maximum, and the smaller one of each pair is compared against the current minimum. This way you do 3 comparisons for every 2 elements, which is slightly more efficient than Arpit's answer (2 comparisons for each element).

    In code:

    #include <stdio.h>
    
    int main(int argc, char **argv) {
        int a, b, c, d;
        printf("Enter four integers (separated by space): ");
        scanf("%d %d %d %d", &a, &b, &c, &d);
    
        int max, min;
        if (a > b) {
           max = a;
           min = b;
        }
        else {
           max = b;
           min = a;
        }
    
        if (c > d) {
           if (c > max) {
              max = c;
           }
           if (d < min) {
              min = d;
           }
        }
        else {
           if (d > max) {
              max = d;
           }
           if (c < min) {
              min = c;
           }
        }
        printf("max = %d, min = %d\n", max, min);
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-10 12:05
    int max(int a, int b) {
        return a > b ? a : b;    
    }
    
    int max_of_four(int a, int b, int c, int d) {
        return max(a, max(b, max(c, d)));
    }
    
    int main() {
        int a, b, c, d;
        scanf("%d %d %d %d", &a, &b, &c, &d);
        int ans = max_of_four(a, b, c, d);
        printf("%d", ans);
    
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题