2009-11-22 89 views
-11

任何人都可以告訴我如何使用遞歸編寫乘法函數(在C)?使用遞歸編碼整數乘法函數(在C中)

+10

請提供一些證據證明您至少已經嘗試過這個問題。這不是一個家庭作業網站。我們在這裏幫助,而不是爲你做你的工作。 – 2009-11-22 18:10:21

+8

2009年最不合適使用遞歸的優勝者。 – shoosh 2009-11-22 18:17:16

回答

2

將它重複添加到自身N次。也就是說,如果你想用N乘以一個數..

5

下面是函數:

int mulitplication(x,y) 
{ 
    if (y==0) 
    { 
     return 0; 
    } 
    else 
     return x+multiplication(x,y-1); 
    } 
} 
+0

嘿......你贏了46秒! :) – Randolpho 2009-11-22 18:14:02

+1

StackOverflow,我們來了! XD – 2009-11-22 18:14:22

+1

@Vilx:Maddy *說*遞歸。它只會爲非常大的乘法器(y的值)堆棧溢出 – Randolpho 2009-11-22 18:15:18

3

你必須,如果你想很好的幫助是方式更加具體。但這裏有一個在C遞歸函數相乘兩個正整數:

int multiply(int multiplicand, int multiplier) 
{ 
    if(multiplier == 0) 
    { 
    return 0; 
    } 
    return multiply(multiplicand, multiplier - 1) + multiplicand; 
} 

喬納森·萊弗勒寫道:如果乘數爲負?

確定:

int multiply(int multiplicand, int multiplier) 
{ 
    if(multiplier == 0) 
    { 
    return 0; 
    } 
    if(multiplier < 0) 
    { 
    return multiply(multiplicand, multiplier + 1) - multiplicand; //Edit: changed "+ multiplicand" to "- multplicand" 
    } 
    return multiply(multiplicand, multiplier - 1) + multiplicand; 
} 

馬克拜爾斯說:負版本仍然是錯誤的。

抱怨,發牢騷。爲我的記憶力而寫作而無需測試。這個測試了許多整數範圍,負值和正值,奇數和偶數。應該適用於任何整數值。 Merry Wintereenmas。

+2

如果乘數是負數? – 2009-11-22 18:17:29

+1

負面版本仍然是錯誤的。 – 2009-11-22 18:33:07

1

如果我沒有錯,是這樣的...

int mul(int a, int b) 
{ 
if(b==1) 
return a; 
else 
return a+mul(a,b-1); 
} 
+0

hm b <= 0 ;-) – 2009-11-23 08:27:19

9

OK,讓我們的是原來的。 :)

unsigned int mul(unsigned int a, unsigned int b) 
{ 
    if (!a || !b) return 0; 
    if (a == 1) return b; 
    if (b == 1) return a; 

    return mul(a-1, b-1)+a+b-1; 
} 
+0

哦,我明白你在那裏做了什麼。 – Randolpho 2009-11-22 18:21:50

+0

如果兩個數字都是負數,會發生笏? -3 * -3 = 9; 但它不工作,如果兩者都是負數... – Madhan 2009-11-22 18:47:02

+6

你好? **未簽名** int? – 2009-11-22 18:51:53

2

替代版本:

int mulInner(int a, int b, int c) 
{ 
    if (b == 0) 
     return a*c; 
    return mulInner(a, b-1, c+1); 
} 
int mul(int a, int b) 
{ 
    return multInner(a, b, 0); 
} 

嘿,他沒有說不要用operator* ...

+0

額外的信貸:不支持負數! – shoosh 2009-11-22 18:22:49

9

如果你真的想打動你的同事和你的老師,提交 - 這是遞歸和快速!

int mul(int a, int b) 
{ 
    if (a < 0) return -mul(-a,b); 
    if (b < 0) return -mul(a,-b); 
    if (b == 0) return 0; 
    return (mul(a,b>>1)<<1)+(b&1?a:0); 
} 

添加:作爲一個額外的好處是,這個正確處理二者陽性,陰性和0值。

+0

優秀的答案,先生你可以解釋僞代碼背後的邏輯? – Madhan 2009-11-22 19:09:37

+7

不是。 XD每當我給作業回答時,我要麼用僞代碼給它,給它不完整,要麼給它如此扭曲和「聰明」,以致要求它的人沒有機會解釋它是如何工作的(如果他/她決定把它作爲他/她自己)。 :) – 2009-11-22 19:34:07

+0

嗯...看起來不像我僞造的代碼。這看起來是一個相當正確的答案。弄清楚它是如何工作的,然後解釋給你的老師。 – 2009-11-23 00:04:31

2

這隻適用於第一個操作數大於零的情況,但至少它很短並且令人困惑。;)

int mul(int a, int b) { 
    return b + (--a ? mul(a, b) : 0); 
} 

但是,我不知道該評價順序定義,所以你可能必須移動減量表達外:

int mul(int a, int b) { 
    a--; 
    return b + (a ? mul(a, b) : 0); 
} 
1
int mul(int a,int b) 
{ 
    if(b==1) 
     return a; 
    else 
     return(a+(mul(a,b-1))); 
} 
0

50個字符:

m(x,y){return!y?0:rand()%2?m(x,y+1)-x:m(x,y-1)+x;} 

我確實希望我可以使用rand()函數(特別是以這種方式)和一般syn稅收優惠。根據你的系統庫和月亮的相位,要注意的是,遞歸會導致參數值高的段錯誤,比如計算2 × 3