【问题标题】:Divide a number by 3 without using division, multiplication or modulus不使用除法、乘法或模数将一个数除以 3
【发布时间】:2015-07-29 14:18:51
【问题描述】:

不使用/%* 运算符,编写一个将数字除以3 的函数。itoa() 可用。

以上是我在一次采访中被问到的,我真的想不出答案。我想将数字转换为字符串并添加所有数字,但这只会告诉我数字是否可整除。或者,通过重复减法,它也可以告诉我余数。但是,如何获得除法商?

【问题讨论】:

标签: algorithm


【解决方案1】:

下面的代码接受 2 个整数,并将第一个除以第二个。它支持负数。

int divide (int a, int b) {
    if (b == 0)
        //throw division by zero error

    //isPos is used to check whether the answer is positive or negative
    int isPos = 1;
    //if the signs are different, the answer will be negative
    if ((a < 0 && b > 0) || (a > 0 && b < 0))
        int isPos = 0;


    a = Math.abs(a);
    b = Math.abs(b);
    int ans = 0;
    while (a >= b) {
        a = a-b;
        ans++;
    }
    if (isPos)
        return 0-ans;
    return ans;
}

【讨论】:

    【解决方案2】:

    根据itoa,数字是整数。

    int divide(int a, int b)
    {
      int n=0;
      while(1)
      {
        a-=b;
        if(a<b)
        {
          n=n+1;
          return n;
        }
        else
         n=n+1;
      }
    }   
    

    只计算 a 中 b 的多少次减去它

    编辑:移除限制

    【讨论】:

      【解决方案3】:

      “计算你减去 3 的次数”算法需要 theta(|input|) 步骤。您可能会争辩说 theta(|input|) 对于 32 位整数很好,在这种情况下,为什么要进行任何编程?只需使用查找表。但是,有更快的方法可用于更大的输入。

      您可以对商执行二进制搜索,通过将 q+q+q 与输入进行比较来测试候选商 q 是太大还是太小。二分查找需要 theta(log |input|) 时间。

      二进制搜索使用除以 2,这可以通过移位运算符而不是 / 来完成,或者如果移位运算符太接近除法,您可以自己在位数组上实现这一点。


      通过尝试 (n>>2) + ( n>>4) + (n>>6) + ... 但是这会产生错误的答案 n=3,6,7,9, 11, 12, 13, 14, 15, 18, ... 它是对于 n=15,30,31,39,.. 关闭 2。一般来说,这是关闭 O(log n)。对于 n 个非负数,

      (n>>2) + (n>>4) + (n>>6) + ...  = (n-wt4(n))/3
      

      其中 wt4(n) 是 n 的基数 4 位的总和,右侧的 / 是精确的,而不是整数除法。我们可以通过将 wt4(n)/3 添加到 (n>>2)+(n>>4)+(n>>6)+ 来计算 n/3... 我们可以计算 n 的基数 4 位,因此wt4(n) 只使用加法和右移。

      int oneThirdOf(int n){
          if (0<=n && n<3)
              return 0;
          if (n==3)
              return 1;
          return sum(n) + oneThirdOf(wt4(n));
      }
      
      // Compute (n>>2) + (n>>4) + (n>>6) + ... recursively.
      int sum(int n){
          if (n<4)
              return 0;
          return (n>>2) + sum(n>>2);
      }
      
      // Compute the sum of the digits of n base 4 recursively.
      int wt4(int n){
          if (n<4)
              return n;
          int fourth = n>>2;
          int lastDigit = n-fourth-fourth-fourth-fourth;
          return wt4(fourth) + lastDigit;
      }
      

      这也需要 theta(日志输入)步骤。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-29
        • 2019-06-01
        • 1970-01-01
        • 2021-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多