85 计算多项式的值
作者: Turbo时间限制: 1S章节: 循环
问题描述 :
计算并输出当x<0.97时下列多项式的值,直到最后一项的绝对值小于threshold(该项不包括在多项式的结果中)为止。
image.png
输入说明 :
可输入多组测试数据,每组一行,每组包括两个实数,第一个为x(0.2≤x <0.97),第二个为threshold(≥0.000001),中间以空格分隔。
输出说明 :
对于每组测试数据,输出一行,为计算出的结果,保留6位小数。输出的结果前后均无空格。两组运算结果之间为空行。
输入范例 :
0.2 0.000001
0.21 0.000001
输出范例 :
1.095445
1.100000
#include<stdio.h>
#include<math.h>
double jiec(int n){
double count=1.0;
for(;n>=2;n--)
count=n*count;
return count;
}
int main(){
double x,threshold,end,s,temp;
int i,j;
while(scanf("%lf%lf",&x,&threshold)!=EOF){
s=0.0;
end=1.0;
i=1;
while(fabs(end)>=threshold){
s=s+end;
if(i==1) {end=0.5*x;i++;}
else{
temp=0.5;
for(j=1;j<i;j++)
temp=temp*(0.5-j);
end=(temp/jiec(i))*pow(x,i);
i++;
}
}
printf("%.6lf\n",s);
printf("\n");
}
return 0;
}