简体   繁体   English

在不使用C语言中使用if..else语句的情况下查找最大值

[英]Finding Max Value without the use of if..else statement in C language

I'm trying to find max value of three integers without the use of if...else statement, when I was doing some search, I came across this lines of code.. Besides the abs function, all the other lines I could understand.Can anyone explain how the abs function in the code works? 我试图在不使用if ... else语句的情况下找到三个整数的最大值,当我进行一些搜索时,遇到了这行代码。除了abs函数,其他所有行我都能理解谁能解释代码中的abs函数如何工作?

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

int main()
{
    int x, y, z, result, max;

    printf("\nInput the first integer: "); 
    scanf("%d", &x);

    printf("\nInput the second integer: ");
    scanf("%d", &y);

    printf("\nInput the third integer: ");
    scanf("%d", &z);

    result=(x+y+abs(x-y))/2;
    max=(result+z+abs(result-z))/2;

    printf("\nMaximum value of three integers: %d\n", max);

    return 0;
}

(a+b+abs(ab))/2

If a>b then this expression becomes (a+b+(ab))/2 which is a . 如果a>b则该表达式变为(a+b+(ab))/2 ,即a
If b>a then this expression becomes (a+b-(ab))/2 which is b . 如果b>a则该表达式变为(a+b-(ab))/2 ,即b

So this expression just gives you the maximum of two values. 因此,此表达式只为您提供两个值中的最大值。

In order to get the maximum of three values you just take the maximum of the maximum of the first two and the third value. 为了获得三个值中的最大值,您只需取前两个和第三个值中的最大值即可。 max(a,b,c) = max(max(a,b),c)

The man page for abs() states, abs()的手册页指出,

The abs() function computes the absolute value of the integer argument. abs()函数计算整数参数的绝对值。

Also, 也,

Returns the absolute value of the integer argument, of the appropriate integer type for the function. 返回该函数的适当整数类型的整数参数的绝对值。

So, if you know basic math then you'd understand that this function returns the absolute value of the expression. 因此,如果您了解基本数学知识,那么您将了解该函数返回表达式的绝对值。

Additionally, over a longer term stop following such strange (and stupid) exercises. 此外,从长远来看,停止这种奇怪(愚蠢的)练习。 These are only helpful for building up the logic, later, these are all useless. 这些仅有助于建立逻辑,以后,这些都是无用的。

As a pun, following the problem literally , I would have done it this way: 作为一个双关语,按照字面意思解决这个问题,我会这样做:

// So, `if..else` statements are banned, right! Take that!
result = (a > b) ? a : b;
max = (result > c) ? result : c;

Remember, reading man pages for a function always helps. 记住,阅读功能手册页总是有帮助的。

Simply use the if statement. 只需使用if语句。 Writing C code without if statements is harmful and silly practice. 编写没有if语句的C代码是有害而愚蠢的做法。 If you try to minimize branches in the code, then that's another story, but that's not really a task for a beginner. 如果您尝试最小化代码中的分支,那将是另一回事,但这对初学者而言并不是真正的任务。


That being said, a solution to the problem is to implement a truth table and use boolean algebra with the > operator. 话虽如此,解决该问题的方法是实现一个真值表,并将布尔代数与>运算符一起使用。 Learning how to implement such a table is useful practice (as opposed to avoiding if statements), since boolean algebra has many uses in programming. 学习布尔值代数在编程中有许多用途,因此学习如何实现这样的表是有用的做法(与避免if语句相反)。

In this case, the truth table is (assuming no values are equal): 在这种情况下,真值表为(假设没有值相等):

           x>y  x>z  y>z
x largest   1    1    -
y largest   0    -    1
z largest   -    0    0

Where - means "don't care". 其中-表示“无关”。

This can be implemented as a 3D array in C, where each index is the result of the corresponding > comparison. 可以将其实现为C语言中的3D数组,其中每个索引都是对应的>比较的结果。 That is: 那是:

array [2]  [2]  [2] 
//    x>y  x>z  y>z

This will however contain some cases that never can be true and the assumption is that no values are equal. 但是,这将包含一些永远不可能为真的情况,并且假定没有值相等。 You can implement it like this: 您可以这样实现:

#include <stdio.h>

int main()
{
  const char* const largest [2]  [2]  [2] =
  //                        x>y  x>z  y>z
  {
    { // x<y
      { // x<z
        "z", // y<z
        "y"  // y>z
      },
      { // x>z
        "-", // y<z, not possible
        "y", // y>z
      }
    },
    { // x>y
      { //x<z
        "z", //y<z
        "-"  //y>z, not possible
      },
      { //x>z
        "x", // y<z
        "x", // y>z
      },
    },
  };

  const int use_cases [6][3] = 
  {
    {1,2,3},
    {1,3,2},
    {2,1,3},
    {2,3,1},
    {3,1,2},
    {3,2,1}
  };

  for(int i=0; i<6; i++)
  {
    int x = use_cases[i][0];
    int y = use_cases[i][1];
    int z = use_cases[i][2];

    printf("x:%d, y:%d, z:%d, largest: %s\n", x, y, z, largest[x>y][x>z][y>z]);
  }

  return 0;
}

Output: 输出:

x:1, y:2, z:3, largest: z
x:1, y:3, z:2, largest: y
x:2, y:1, z:3, largest: z
x:2, y:3, z:1, largest: y
x:3, y:1, z:2, largest: x
x:3, y:2, z:1, largest: x

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

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