c*********t 发帖数: 2921 | 1 Write a function to perform integer division without using either the / or *
operators. Find a fast way to do it. | s*********t 发帖数: 1663 | 2 就是让你把小学二年级用的除法拿程序写出来
*
【在 c*********t 的大作中提到】 : Write a function to perform integer division without using either the / or * : operators. Find a fast way to do it.
| c*********t 发帖数: 2921 | 3 能不能具体的说说?
比如 98 除以 5, 程序如何写?
谢谢!
【在 s*********t 的大作中提到】 : 就是让你把小学二年级用的除法拿程序写出来 : : *
| c******t 发帖数: 1500 | 4 用减法
int divide(int target, int divisor)
{
int result = 0;
while (target - divisor > 0)
{
target -= divisor;
++result;
}
return result;
}
【在 c*********t 的大作中提到】 : 能不能具体的说说? : 比如 98 除以 5, 程序如何写? : 谢谢!
| c****p 发帖数: 6474 | 5 google booth
*
【在 c*********t 的大作中提到】 : Write a function to perform integer division without using either the / or * : operators. Find a fast way to do it.
| c****p 发帖数: 6474 | 6 假设A/B:
#include
int main()
{
int A, B, tB, tA, Q, R;
scanf("%d", &A);
scanf("%d", &B);
tB = B;
if (A
{
Q = 0;
R = A;
}
else
{
while(A>tB)
{
tB<<=1;
}
tB >>= 1;
tA = A;
Q = 0;
while(tB >= B)
{
Q <<= 1;
if(tA >= tB)
{
tA -= tB;
Q += 1;
}
tB >>= 1;
}
R = tA;
}
printf("%d / %d = %d ... %d\n", A, B, Q, R);
return 0;
}
*
【在 c*********t 的大作中提到】 : Write a function to perform integer division without using either the / or * : operators. Find a fast way to do it.
| c****p 发帖数: 6474 | 7 假设A和B都正。
这个不是booth算法,但是比一个个减要快。
booth带预测,运算量更小一些。
可以扩展为支持+和-的运算
【在 c****p 的大作中提到】 : 假设A/B: : #include : int main() : { : int A, B, tB, tA, Q, R; : scanf("%d", &A); : scanf("%d", &B); : tB = B; : if (A: {
|
|