不使用乘法、除法和取模运算符来进行两个整数的除法

不使用乘法、除法和取模运算符来进行两个整数的除法

在这个问题中,我们只需要将两个整数相除,而不需要使用乘法、除法和取模运算符。尽管我们可以使用加法、乘法或位操作。

问题陈述指出我们将得到两个整数 x 和 y。在不使用乘法、除法或取模运算符的情况下,我们需要确定 x 除以 y 后的商。

示例

输入:x=15,y=5

输出:3

输入:x=10,y=4

输出:2

输入:x=-20,y=3

输出:-6

方法

方法1(使用简单的数学)

在这种方法中,我们将使用一个简单的数学算法。下面是我们要遵循的步骤的分步说明 –

我们将从被除数(即 x)中不断减去除数(即 y),直到 x 大于或等于 y。

当 y 大于 x 时,即除数大于被除数,被除数变为余数,减法次数变为商。

将减法执行的次数存储在变量中并返回它,这是我们想要的输出。

示例

下面是上述算法的 C++ 实现 &minnus;

#include #include using namespace std;long long division(long long a,long long b) // where a is dividend and b is divisor{   long long sign=1;   if((a<0) ^( b=n){      m=m-n;      count++;   }   if(sign==-1) // when sign is negative   {      count=-count;   }   return count;} int main(){   long long a=-21474;   long long b=2;   long long val=division(a,b);   cout<<val<<endl;   return 0;}

输出

-10737

时间复杂度:O(a/b)

空间复杂度:O(1)

方法 2(使用位操作)

由于任何数字都可以用 0 或 1 的形式表示,因此可以使用移位运算符以二进制形式表示商。

使用 for 循环迭代除数从 31 到 1 的位位置。

找到除数即 b

验证下一个位置时,将结果添加到 temp 变量中,以确保 temp+(b

每次通过计算商来更新商OR 1

更新相应符号后返回商。

示例

下面是上述方法的 C++ 实现 –

#include #include using namespace std;long long division(long long a,long long b) // where a is dividend and b is divisor{   long long sign=1;   if((a<0) ^( b= 0; --j){         if (temp + (n << j) <= m){         temp += n << j;         count |= 1L << j;      }   }   if(sign==-1) // when sign is negative   {      count=-count;   }   return count;   } int main(){   long long a=49;   long long b=5;   long long val=division(a,b);   cout<<val<<endl;   a=-18,b=5;   cout<<division(a,b);      return 0;}

输出

9-3

时间复杂度:O(log(a))

空间复杂度:O(1),因为它不使用额外的空间。

方法 3(使用对数函数)

在这种方法中,我们将使用一个简单的对数函数来计算商。

众所周知,

$$mathrm{In(frac{a}{b}):=:In(a):-:In(b)}$$

可以进一步修改为

$$mathrm{frac{a}{b}:=:e^{(In(a):-:In(b))}}$$

因此,这是使用这种有效方法解决给定问题的基本思想。

下面是我们将要遵循的方法的分步说明 –

如果其中一个(即被除数或除数)为 0,我们将返回 0。

现在,我们将使用异或函数 (XOR) 检查符号,以将符号存储在变量中。

如果除数为 1,则直接返回被除数。

现在,声明一个变量并使用 exp 函数和 log 函数。

Log 和 exp 是 C++ 中的内置函数。 Log 函数返回输入数字的自然对数值,exp 返回等于 e 加上输入值的值。

示例

下面是上述方法的 C++ 实现 –

#include #include using namespace std;long long int divide(long long int a,long long int b){   long long int sign=1;   if(a==0||b==0) // when a is zero or b is zero   {      return 0;   }   if((a>0) ^ (b>0)) // - ^ - = +,+ ^ - = - , - ^ + = - , + ^ + = +   {      sign=-1;   }   if(b==1) // when b is 1 then it will return a example 51/1 = 51   {      sign==-1?-a:a;      return a;   }   long long int m=abs(a);   long long int n=abs(b);      //log function return the logarithmic value of the entered value with base e i.e. natural log of the entered value   //exp function return the value equal to e^(entered value)   long long int ans =exp(log(m) - log(n)) + 0.0000000001;       // if it gives the value in decimal we will add from 0.0000000001 to account for accuracy errors   if(sign==-1) // when sign is negative return the negative ans   {      return -ans;   }   return ans;   }int main(){   long long int ans=divide(47,-9);   cout<<ans<<endl;      return 0;}

输出

-5

时间复杂度:O(1),,因为执行该操作需要恒定的时间。

空间复杂度:O(1),因为它不使用额外的空间。

结论

在本文中,我们学习在不使用乘法、除法或取模运算符的情况下将两个整数相除。我们学会了用不同的方法以不同的效率解决问题。他们使用简单的数学、位操作和对数函数。其中,使用对数函数是最有效的方法,因为它的时间复杂度为 O(1),是所有方法中最小的。

我希望这篇文章可以帮助您解决有关该主题的所有概念。

以上就是不使用乘法、除法和取模运算符来进行两个整数的除法的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1445578.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 22:38:50
下一篇 2025年12月17日 18:13:30

相关推荐

  • 使用pthread在C/C++中实现矩阵的加法和减法

    这里我们将看到如何使用多线程环境执行矩阵加法和减法。 pthread用于在C或C++中同时执行多个线程。 有两个矩阵A和B。每个矩阵的阶数为(m x n)。每个线程将获取每一行,并执行加法或减法。因此,对于 m 行,有 m 个不同的线程。 示例 #include#include #include #…

    2025年12月17日
    000

发表回复

登录后才能评论
关注微信