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

后端 未结 18 2418
北海茫月
北海茫月 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:47

    This is C code has only 4 if statements. It moves max number to d position and min number to a position. Values b and c are not properly arranged within a sequence, but since requirements ask for min and max this code completes a job:

    #include <stdio.h>
    
    
        int main() {
            int a, b, c, d, temp;
            printf("Enter four digits: ");
            scanf("%d %d %d %d", &a, &b, &c, &d);
            if ( a > b){
                temp = a; a = b ; b = temp;
            }
            if ( c > d){
                temp = c; c = d ; d = temp;
            }
            if ( b > d ){
                temp = b; b = d; d = temp;
            }
            if ( a > c){
                temp = a; a = c ; c = temp;
            }
            printf("Max %d\nMin %d\n", d, a);
    
            return 0;
        }
    
    0 讨论(0)
  • 2020-12-10 11:48

    I saw this answer that used for loop and if else decision statement , however I will post a solution that I suppose will run faster and will use only four variables. So here it goes...

    #include<stdio.h>
    void main()
    {
    int a,b,c,d;
    printf("Enter four numbers of your choice");
    scanf("%d%d%d%d",&a,&b,&c,&d);
    a>b&&a>c?a>d?printf("%d",a):printf("%d" ,d):(b>c&&b>d)?printf("%d",b):c>d?printf("%d", c):printf("%d",d);
    }
    
    0 讨论(0)
  • 2020-12-10 11:50

    Try something like this

    int main(void) {
        int a=-2,b=-3,c=-4,d=-5;
        int max=a,min=a;
    
        if(b>max){
            max=b;
        }else if(b<min){
            min=b;
        }
        if(c>max){
            max=c;
        }else if(c<min){
            min=c;
        }
        if(d>max){
            max=d;
        }else if(d<min){
            min=d;
        }
        printf("max: %d min : %d",max,min);
        return 0;
    }
    

    Demo

    0 讨论(0)
  • 2020-12-10 11:51
    #include <stdio.h>
    
    int max_of_four(int a, int b, int c, int d){
        int mx_A_B = (a > b) * a + (a <= b) * b;
        int mx_C_D = (c > d) * c + (c <= d) * d;
    
        return (mx_A_B > mx_C_D) * mx_A_B + (mx_A_B <= mx_C_D) * mx_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)
  • 2020-12-10 11:54

    This doesn't use any loops and only 4 conditionals:

    #include <stdio.h>
    
    int main(void)
    {
      int i, j, k, l, t, max, min;
    
      printf("Enter four integers: ");
      scanf("%d%d%d%d", &i, &j, &k, &l);
    
      if (i < j) {
        t = i;
        i = j;
        j = t;
      }
      if (k < l) {
        t = k;
        k = l;
        l = t;
      }
      max = (i >= k) ? i : k;
      min = (j <= l) ? j : l;
    
      printf("Largest: %d\n", max);
      printf("Smallest: %d", min);
    
      return 0;
    }
      
    
      
    
    0 讨论(0)
  • 2020-12-10 11:57

    Here is a solution with no if or elseif or function or macro, but using bit-shift and subtraction instead; only using a single for loop:

    #include <stdio.h>
    int main(){
    
      int num , max, min;
    
      printf("Enter four numbers: ");
      scanf("%d", &num);
      max = min = num;
    
      for(int i = 0; i < 3; i++)
      { 
        scanf("%d", &num);
        max = max * (1 - ( (max-num) >> 31) )
            + num *      ( (max-num) >> 31);
        min = min * (1 - ( (num-min) >> 31) )
            + num *      ( (num-min) >> 31);
      }
    
      printf("\n%d %d", max, min);
      return 0;
    }
    

    The (max-num) >> 31) operation captures the sign of the difference, which when multiplied by the second number yields the minimum value of the comparison.

    This comes from an old SQL coding trick from the days before there was a CASE WHEN construct in that language.

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