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>
int main(){
double x,threshold,sum,temp;
int n,flag=0;
while(scanf("%lf %lf",&x,&threshold)!=EOF){
sum=1.0;
n=2;
if(flag==1){
printf("\n");
}
flag=1;
if(threshold>1){
printf("0.000000\n");
continue;
}
temp=0.5*x;
while(fabs(temp)>=threshold){
sum+=temp;
temp*=1.0*(0.5-n+1)*x/n;
n++;
}
printf("%.6f\n",sum);
}
return 0;
}