递归函数计算x^n#include double fun(int n,double x);int main(void){\x05int n;\x05double x,root;\x05scanf("%lf%d",&x,&n);\x05root = fun(n,x);\x05printf("Root = %0.2f\n",root);}double fun(int n,double x){\x05int y;\x05\x05\x05if(n==1) return x;\x0

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/08 07:42:15

递归函数计算x^n#include double fun(int n,double x);int main(void){\x05int n;\x05double x,root;\x05scanf("%lf%d",&x,&n);\x05root = fun(n,x);\x05printf("Root = %0.2f\n",root);}double fun(int n,double x){\x05int y;\x05\x05\x05if(n==1) return x;\x0
递归函数计算x^n
#include
double fun(int n,double x);
int main(void)
{
\x05int n;
\x05double x,root;
\x05scanf("%lf%d",&x,&n);
\x05root = fun(n,x);
\x05printf("Root = %0.2f\n",root);
}
double fun(int n,double x){
\x05int y;
\x05
\x05
\x05if(n==1) return x;
\x05else y=fun(n-1,x)*x;
\x05
\x05return y;
\x05
}
这段程序提交上去显示答案错误
但是把后面改成
double fun(int n,double x){
\x05int y;
\x05
\x05
\x05if(n==1) return x;
\x05else return fun(n-1,x)*x;
\x05
}
就对了
为什么呢.

递归函数计算x^n#include double fun(int n,double x);int main(void){\x05int n;\x05double x,root;\x05scanf("%lf%d",&x,&n);\x05root = fun(n,x);\x05printf("Root = %0.2f\n",root);}double fun(int n,double x){\x05int y;\x05\x05\x05if(n==1) return x;\x0
fun函数是double类型的,而返回的y是int类型,二者不匹配,将y强制为double类型就可以了