我正在嘗試將兩個數字相乘並且不應該使用*運算符的代碼。我編寫了這段代碼,但沒有給出預期的解決方案。乘法而不使用*運算符
#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;
}
任何人都可以請解釋什麼是錯的代碼?
迫不及待。接下來的時間請自己做。 – Maroun
提示:您的代碼可能適用於像'2^n'這樣的輸入。 – devnull
難道不是很複雜嗎?我的意思是3 * 2它只是3 + 3 => for(int i = 0; i Dabo