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,sum=1.0,threshold,temp;
int i=2;
while(scanf("%lf %lf",&x,&threshold)!=EOF){
if(threshold>1) printf("0.000000\n"); **//注意大于1的特殊情况**
else{
temp=0.5*x;
while(fabs(temp)>=threshold){ **//浮点数比较记得用fabs**
sum+=temp;
temp=temp*(0.5-i+1)*x*1.0/i;
i++;
}
printf("%.6lf\n",sum);
}
printf("\n");
sum=1.0,
i=2;
}
return 0;
}
来源:CSDN
作者:running snail
链接:https://blog.csdn.net/qq_43587411/article/details/104619785