我正在尝试将两个数字相乘并且不应使用*运算符的代码。我写了这段代码,但没有给出预期的解决方案。
#include<stdio.h>
#include<stdlib.h>
int main(int ar,char *arg[])
{
long int a;
long int b;
sscanf(arg[1],"%ld",&a);
sscanf(arg[2],"%ld",&b);
long int count=0;
long int temp=b;
long int prod=0;
while(temp>0)
{
count=0;
while(temp/2>0)
{
temp/=2;
count++;
}
prod+=a<<(count);
temp=b-(1<<count);
}
//printf("%ld %d\n",count,1<<count);
printf("%ld\n",prod);
return 0;
}
谁能解释一下代码中的错误吗?
以这种方式编辑while循环。
while(temp>0)
{
count=0;
while(temp/2>0)
{
temp/=2;
count++;
}
prod+=a<<(count);
temp=b-(1<<count);
b=temp //<--
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句