查找三个整数中最大的整数的最有效方法 [英] Most efficient way to find the greatest of three ints

查看:78
本文介绍了查找三个整数中最大的整数的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的伪代码.

function highest(i, j, k)
{
  if(i > j && i > k)
  {
    return i;
  }
  else if (j > k)
  {
    return j;
  }
  else
  {
    return k;
  }
}

我认为这可行,但这是C ++中最有效的方法吗?

I think that works, but is that the most efficient way in C++?

推荐答案

要找到最大的整数,您需要准确地查看3个整数,多不多不少.您正在看6与3比较.您应该能够在3次和2次比较中做到这一点.

To find the greatest you need to look at exactly 3 ints, no more no less. You're looking at 6 with 3 compares. You should be able to do it in 3 and 2 compares.

int ret = max(i,j);
ret = max(ret, k);
return ret;

这篇关于查找三个整数中最大的整数的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆