简体   繁体   English

如何在 C 中不使用 if/else 或 switch case 计算三个数字中的最大值?

[英]How can I calculate greatest of three numbers without using if/else or switch case in C?

I want to calculate greatest of given three integer numbers in C programming without using any if / else condition or switch case.我想在 C 编程中计算给定三个整数中的最大值,而不使用任何if / else条件或switch case。 For example in this way.例如以这种方式。

#include <stdio.h>
#include <stdlib.h>

int main() {

    int num1, num2, max;
    scanf("%d %d", &num1, &num2);
    max = (num1 + num2 + abs(num1 - num2)) /2;
    printf("%d\n", max);
    
    return 0;
}

Here is my code to calculate the max of given two integer numbers.这是我的代码来计算给定两个整数的最大值。

You can call the max function twice:您可以调用 max 函数两次:

#include <stdio.h>
#include <stdlib.h>

int main() {

    int num1, num2, num3, max;
    scanf("%d %d %d", &num1, &num2, &num3);
    max = (num1 + num2 + abs(num1-num2))/2;
    max = (max + num3 + abs(max-num3))/2;
    printf("%d\n", max);
    
    return 0;
}

You can use ternary expression as well:您也可以使用三元表达式:

#include <stdio.h>

#define max2(a,b) (((a) > (b)) ? (a) : (b))
#define max3(a,b,c) max2(max2((a), (b)), (c))

int main() {
    printf("%d\n", max3(10, -10, 20));

    return 0;
}

Your question makes no sense: the meaning of abs(x) is (more or less):您的问题毫无意义: abs(x)的含义是(或多或少):

int abs(int x)
{
    if (x>0) 
      return x;
    else return -x;
}

So the simple usage of abs() implies an if -clause.所以abs()的简单用法意味着一个if子句。

Instead of if / else statements, you can use the ternary operator:代替if / else语句,您可以使用三元运算符:

#include <stdio.h>

int main() {
    int num1, num2, num3, max;
    if (scanf("%d%d%d", &num1, &num2, &num3) == 3) {
        max = (num1 < num2) ? num1 : num2;
        max = (max < num3) ? max : num3;
        printf("%d\n", max);
    }
    return 0;
}

If the ternary operator is not allowed, you can use comparison operators, which may generate branchless code anyway:如果不允许使用三元运算符,则可以使用比较运算符,这可能会生成无分支代码:

#include <stdio.h>

int main() {
    int num1, num2, num3, max;
    if (scanf("%d%d%d", &num1, &num2, &num3) == 3) {
        max = (num1 >= num2) * num1 + (num1 < num2) * num2;
        max = (max >= num2) * max + (max < num2) * num2;
        printf("%d\n", max);
    }
    return 0;
}

Note that you cannot use num1 + num2 nor num1 - num2 , not even -num1 nor num2 because there operations may cause arithmetic overflow with undefined behavior.请注意,您不能使用num1 + num2num1 - num2 ,甚至不能使用-num1num2 ,因为这些操作可能会导致算术溢出并导致未定义的行为。

Also note that you can abuse the rules by replacing if statements with while or for statements:另请注意,您可以通过将if语句替换为whilefor语句来滥用规则:

#include <stdio.h>

int main() {
    int num1, num2, num3;
    if (scanf("%d%d%d", &num1, &num2, &num3) == 3) {
        for (max = num1; max < num2; max = num2)
            continue;
        while (max < num3)
            max = num3;
        printf("%d\n", max);
    }
    return 0;
}

without using any if/else condition or switch case不使用任何 if/else 条件或 switch case

#define m(a,b,c)a>b?a>c?a:c:b>c?b:c

35 bytes, I win. 35 字节,我赢了。

You may use something similar to this:您可以使用类似的东西:

#include <stdio.h>

int a,b,c, max_num;


int maxof3(int num1, int num2, int num3)
{
    return(max(max(num1, num2), num3));
}

int main()
{
    scanf("%d %d %d", &a, &b, &c);
    max_num = maxof3(a, b, c);
    printf("%d\n", max_num);
    
    return 0;
}

Since you asked different ways, and not efficient ways you can sort them using the qsort library function and then return the last element.由于您询问了不同的方式,而不是有效的方式,您可以使用qsort库函数对它们进行排序,然后返回最后一个元素。

#include <stdio.h>
#include <stdlib.h>

int cmp (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); }

int main (void) {
   int arr[3];
   scanf("%d %d %d", &arr[0], &arr[1], &arr[2]); //ignoring valid input check, assuming valid input
   qsort(arr, 3, sizeof(int), cmp);
   printf("max: %d",arr[2]);
   return 0 ;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM