我写的为什么死循环?
#include"stdio.h"
#include"math.h"
fun(double x,double n)
{
double a,b,c,d,e,g,h,i,j;
double sum=1,0,f;
d=1,a=2,e=2,b=1;
for(;d<=n;d++,a=a*2,e*=2)
{
i=pow(x,a);
j=pow(-1.0,d);
b=i*j;
g=1.0;
for(h=1.0;h<=e;h++)
{
g=g*h;
}
f=b/g;
sum=sum+f;
}
return sum;
}
main()
{
double x,n,sum;
scanf("%f,%f",&x,&n);
sum=fun(x,n);
printf("%f",sum);
}
3回答
-
望远
#include"stdio.h"
#include"math.h"
double fun(double x,double n)//注意返回值类型
{
double a,b,c,d,e,g,h,i,j;
double sum=1.0,f;
d=1,a=2,e=2,b=1;
for(;d<=n;d++,a=a*2,e*=2)
{
i=pow(x,a);
j=pow(-1.0,d);
b=i*j; g=1.0;
for(h=1.0;h<=e;h++)
{
g=g*h;
}
f=b/g;
sum=sum+f;
}
return sum;
}
main()
{
double x,n,sum;
scanf("%lf,%lf",&x,&n); //注意double数据类型的输入输出格式
sum=fun(x,n);
printf("%lf",sum);
}
-
Anjaxs
#include"stdio.h"
#include"math.h"
/**
* (1*x^0)/1! + -1*x^2/2! + ... + [-1^(n-1)]*[x^2*(n-1)]/n!
**/
double fun(double x,double n)
{
double a, b, c, d, e, g, h, i, j;
double sum = 1.0, f;
d = 1, a = 2, e = 2, b = 1;
for(; d <= n; d++, a = a*2, e *= 2) {
i = pow(x, a);
j = pow(-1.0, d);
b = i*j;
g = 1.0;
for(h=1.0; h <= e; h++) {
g = g*h;
}
f = b/g;
sum = sum + f;
}
return sum;
}
main()
{
double x,n,sum;
scanf("%f,%f",&x,&n);
sum=fun(x,n);
printf("%f",sum);
}可能是这样