乘以七

发布于 2024-11-09 19:27:28 字数 155 浏览 3 评论 0原文

我被问到这个面试问题: 一个数乘以 7 最快的方法是什么? 她告诉我不要使用任何 + 、 - 、 * 、 / 运算符。 紧张中,我无法回答这个问题。

我知道将数字乘以 8 的最快方法是 n<<3 但是n*7能实现吗?

I was asked this interview question:
what is the quickest way to multiply a number by 7.
she told me not to use any of the + , - , * , / operators.
In tense,i could not answer the question.

I know the quickest way to multiply a number by 8 is n<<3
but can n*7 be acheived?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

老旧海报 2024-11-16 19:27:28

假设你的编译器并不糟糕,n*7

Assuming your compiler isn't terrible, n*7.

走过海棠暮 2024-11-16 19:27:28

几乎满足您的所有要求:)

#include <stdio.h>

int mult7(int i)
{
    int res;
    __asm__("imull  $7, %1, %0" : "=r" (res) : "r" (i));
    return res;
}

int main()
{
    printf("%d", mult7(12)); //output: 84
    return 0;
}

Meets almost all your requirements :)

#include <stdio.h>

int mult7(int i)
{
    int res;
    __asm__("imull  $7, %1, %0" : "=r" (res) : "r" (i));
    return res;
}

int main()
{
    printf("%d", mult7(12)); //output: 84
    return 0;
}
锦上情书 2024-11-16 19:27:28

未使用禁止的运算符(+-*/):)

/* multiply by 7 without using the forbidden operators */
/* if `n` is larger than `INT_MAX / 7`
** or smaller than `INT_MIN / 7`
** the behaviour is undefined. */
int mult7(int n) {
  int n7 = n;
  n7 *= 7;
  return n7;
}

No forbidden operators (+, -, *, or /) used :)

/* multiply by 7 without using the forbidden operators */
/* if `n` is larger than `INT_MAX / 7`
** or smaller than `INT_MIN / 7`
** the behaviour is undefined. */
int mult7(int n) {
  int n7 = n;
  n7 *= 7;
  return n7;
}
無處可尋 2024-11-16 19:27:28
n*7 == (n<<3) - n

不确定这是否会比正常乘以 7 更好

n*7 == (n<<3) - n

Not sure if that will be better then normal multiplication by 7 though

伪装你 2024-11-16 19:27:28

正确的答案是 的组合,

a) n*7

因为这正是编译器完全能够自行计算出的微优化类型。

b) the interviewer has a poor understanding of modern optimizing compilers

或者,如果面试官不是一无所知,则与上述 a) 不同的答案表明面试者不了解优化编译器,因此不适合该职位。 :-/

The correct answer is a combination of

a) n*7

because this is exactly the kind of micro-optimization that the compiler is perfectly capable of figuring out on its own.

b) the interviewer has a poor understanding of modern optimizing compilers

Alternatively, if the interviewer isn't clueless, an answer different than a) above suggests that the interviewee does not understand optimizing compilers and is thus a poor candidate for the job. :-/

抱着落日 2024-11-16 19:27:28

我能想到的最好答案是使用 while 循环和每个位的按位运算符编写“+”操作。像这样:


int add(int x, int y) {
  int a, b;
  do {
    a = x & y; b = x ^ y; x = a << 1; y = b;
  }while(a);
  return b;
}

然后对 n 求和,n << 1、n<< 2.

“+”很容易用 while 循环编写,如果您能想出用 while 循环编写“-”的方法(我不知道到底如何做),那么您可以做到(n << 3 ) - n.

来源:http://geeki.wordpress.com/2007/12/12/adding-two-numbers-with-bitwise-and-shift-operators/

The best answer I can come up with is to write "+" operation using while loop and bitwise operators per each bit. Something like this:


int add(int x, int y) {
  int a, b;
  do {
    a = x & y; b = x ^ y; x = a << 1; y = b;
  }while(a);
  return b;
}

And then sum up n, n << 1, n << 2.

"+" is easy to write with while loop, if you can come up with way to write "-" with while loop (which i am not sure how to do exactly) than you can do (n << 3) - n.

source:http://geeki.wordpress.com/2007/12/12/adding-two-numbers-with-bitwise-and-shift-operators/

尴尬癌患者 2024-11-16 19:27:28

很可能是

n*7

或者

(n<<3) - n

It's likely either

n*7

or

(n<<3) - n
荒路情人 2024-11-16 19:27:28

也许这也是一个有效的答案:pow(10, log10(n) + log10(7))

Maybe this is a valid answer as well : pow(10, log10(n) + log10(7))

携君以终年 2024-11-16 19:27:28

哦,你是如此接近!

= (n << 3 - n)

= n * 8 - n

= n(8 - 1)

= n * 7

oh you were so close!

= (n << 3 - n)

= n * 8 - n

= n(8 - 1)

= n * 7

时光病人 2024-11-16 19:27:28

假设面试官正在寻找一种移位和添加的答案,我想你可以使用:

int ans = (num<<2) + (num<<1) + num;

我想这是一种测试面试者是否知道这个特定算法的粗略方法。

Assuming that the interviewer is looking for a shift-and-add kinda answer, I guess you could use:

int ans = (num<<2) + (num<<1) + num;

I guess this a crude way of testing if the interviewee knows of this particular algo.

关于从前 2024-11-16 19:27:28
int main()
   {
        int i;
        int x;
        int n;
        x = n;
        for (i = 2; i < 8; i++)
        {
             n += x;
        }
        return 0;
    }

编辑:转换为c(我认为)并意识到c中没有.=,所以不得不切换到+=。从技术上讲仍然与 + 不同,但有点值得怀疑。

我知道它使用 ++ 但肯定与“+”不同,并且 .= 避免了规定。

int main()
   {
        int i;
        int x;
        int n;
        x = n;
        for (i = 2; i < 8; i++)
        {
             n += x;
        }
        return 0;
    }

EDIT: converted to c (i think) and realized there is no .= in c so had to switch to +=. Still not the same as + technically but a bit questionable.

I know it uses the ++ but that certainly not the same as "+" and .= avoids the stipulation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文