是否有可能找到的最大的两个整数,没有任何比较?我发现了一些方案:

if(!(a/b)) // if a is less than b then division result will be zero.
{
    cout << " b is greater than a";
}
else if (!(a-b)) // we know a is greater than or equal to b now.  check whether they are equal.
{
    cout << "a and b are equal";
}
else
    cout << "a is greater than b";

但是,如果(c)或if(!c)是比较为零。此外,它并不作为负数。事实上我需要一个解决方案,避免任何如果发言。相反,我应该使用关发言和发表的算术运营商。感谢名单.

有帮助吗?

解决方案

减去它们,并使用讨厌位摆弄黑客结果,检查标志 http://graphics.stanford.edu/~seander/bithacks.html

如果其他的程序员都知道你住的地方不这样做在生产代码。

其他提示

下面是没有任何条件分支的乐趣位变换版本。

int g = (int)"greater";
int l = (int)"less";
int e = (int)"equal";

int a = 7;
int b = 10;

char *result = (char*)((((a - b) >> 31) & l) | (((b - a) >> 31) & g) | ((~((a - b) | (b - a))) >> 31) & e);
cout << result;

不是在问题或任何问题的答案由零从除法迄今保护呈现的样本中的一个。为什么地球上你想避免“如果”语句?我怀疑有关家庭作业的问题:运营商

cout << "Maximum is: " << ((a>b)?a:b)

有才好。

这是不可能没有的比较来比较两个数字。你可以掰过来,做一个间接的操作,但你要比较的东西一天结束。信任编译器优化代码,并选择最佳的操作。

你可能利用事实标志的计算 a - b 取决于其数量更大。这是用在许多实现的比较。但是我相信你将永远无法完全避免的比较。在这种情况下,你仍至少需要评估的内容标志旗在处理器。

如果你只是需要显示较低的数字,你也可以使用算术技巧:

result = ((a + b) - sqrt((a - b) * (a - b))) / 2

编辑 呃...你可以使用 switch?

我应该使用关发言和发表的算术运营商。

switch 基本上是一样的链接 if 和如此,它还采用比较。这听起来如果你确实应该只是比较为零看到什么迹象 a - b 已。

char c;
c=0x3D + (!(b/a) && (a-b)) - (!(a/b) && (a-b));
printf("a %c b",c);
(!(a/b) ?  cout << " b is greater than a" : (!(b-a) ? cout << "a and b are equal" :  cout << "a is greater than b") :  cout << "a is greater than b");

这变得有点杂乱虽然

编辑:这是家庭作业

我只是不能看到任何充分的理由这样做:谁愿意没有“如果”编程?

一个可能的答案是:

((A + B)+ ABS(一个-b))/ 2

我猜“ABS”只是隐藏了一个“如果”的地方,就像三元运算符,这只是另一个名称为“如果” ......

在不正当思想:使用函数指针阵列。然后用一些算术和位操作得到一个索引到该阵列。

作为一个毫无意义的运动,这里的实施cond功能的一种方式 - 服务if的目的,假定它(switch?:)不知何故从语言消失了,你使用的C ++ 0x。

void cond(bool expr, std::function<void ()> ifTrue, std::function<void ()> ifFalse)
{
    std::function<void ()> choices[2] = { ifTrue, ifFalse };
    choices[expr == false]();
}

e.g。

cond(x > y,
    /*then*/ [] { std::cout << "x is greater than y"; },
    /*else*/ [] { std::cout << "x is not greater than y"; });

像我说,毫无意义的。

尝试此,测试它,效果很好。

public static int compare(int a, int b)
{
    int c = a - b;
    return (c >> 31) & 1 ^ 1;
}

我认为这种方法比其它方法更好的,可以使用这个逻辑C和Java两种编程语言,但INT应该是4字节的,如果int是2字节的然后使15字节右移,而不是31字节。

enter code here

#include<stdio.h>

main()
{
   int a, b;
   printf("Enter three numbers\n");
   scanf("%d %d", &a, &b);
   printf("Largest number is %d \n",findMax( a,b ));
}
int findMax( int x, int y)
{
  int z = x - y;
  int i  = (z  >>  31)  &  0x1;
  printf("i = %d shift = %d \n", i, (z>>31));
  int  max  =  x - i  *  z;
  return max;
}

要得到最大的数量,而不使用比较/关系运算符

void PrintGreatestNumber(int a, int b)
{
   int [] x = new int[] { -1, 0, 1 };
   int greatestNumber =  ((a+b)+ x[ 1 + ((a-b) >> 31) - (-(a-b) >> 31)] * (a-b)) /2;  
   Console.WriteLine(greatestNumber);
}
void greater(int a, int b) {
    int c = a - b;
    switch(c) {
        case 0:
            cout << "a and b are equal" << endl;
            break;
        default:
            int d = c & (1<<31);
            switch(d) {
                case 0:
                    cout << "a is bigger than b" << endl;
                    break;
                default:
                    cout << "a is less than b" << endl;
            }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top