我不明白爲什麼它工作正常,如果你改變所有的double
爲int
,雙重回報0遞歸,但工作得很好,當INT
是有什麼錯我的printf語句?
您使用%f
或%lf
爲double
,對吧?
/*
double power(double a, int b) that calculates the power ab. You are allowed to use the
multiplication operator *.
*/
#include <stdio.h>
#include <conio.h>
double power(double a,int b)
{
double buffer;
if(b > 1)
{
buffer = a * power(a,b-1);
return buffer;
}
else
return a;
}
int main(void)
{
double a;
int b;
int buffer;
scanf("%f",&a);
scanf("%d",&b);
buffer = power(a,b);
printf("%f",buffer);
getch();
}
嘗試''lf'爲'double','double'爲'buffer'。 –
[cnicutar](http://stackoverflow.com/a/12235250/335858answer)正確地確定了代碼中的兩個問題,這些問題將使您的代碼正常工作,但第三個問題可能會在您的最終用戶代碼顯示時出現,用戶輸入一個非常大的'b':你的程序可能會溢出堆棧,或者花費太長的時間來產生結果,這取決於優化器的設置。你需要閱讀[平方的指數](http://en.wikipedia.org/wiki/Exponentiation_by_squaring)來解決這個問題。 – dasblinkenlight
如果可能,您應該調整編譯器首選項(或*首選項*),以便在可能的情況下警告類型和格式說明符之間的不匹配。 –