输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个整数n (n>=0)和一个双精度浮点数x,输出函数p(n,x)的值(保留2位小数)。[1 (n=0)p(n, x) = [x (n=1)[((2*n-1)*p(n-1,x)-(n-1)*p(n-2,x))/n (n>1)例:括号内是说明输入3 (repeat=3)0 0.9 (n=0,x=0.9)1 -9.8 (n=1,x=-9.8)10 1.7 (n=10,x=1.7)输出p(0, 0.90)=1.00p(1, -9.80)=-9.80p(10, 1.70)=3.05#include <stdio.h>double p(int n, double x);int main(void){int repeat, ri;int n;double x, result;scanf("%d", &repeat);for(ri = 1; ri <= repeat; ri++){scanf("%d%lf", &n, &x);result = p(n, x);printf("p(%d, %.2lf)=%.2lf\n", n, x, result);}}
2 回答
墨色风雨
TA贡献1853条经验 获得超6个赞
#include <stdio.h>
double p(int n, double x);
int main(void)
{
int repeat, ri;
int n;
double x, result;
scanf("%d", &repeat);
for(ri = 1; ri <= repeat; ri++)
{
scanf("%d%lf", &n, &x);
result = p(n, x);
printf("p(%d, %.2lf)=%.2lf\n", n, x, result);
}
}
double p(int n, double x)
{ double result;
if(n==0) result= 1;
else if(n==1) result=x;
else result=((2*n-1)*p(n-1,x)-(n-1)*p(n-2,x))/n;
return result;
}
九州编程
TA贡献1785条经验 获得超4个赞
您这种写法有很大问题,首先用户的输入n是不确定的,主程序是不能这么写的,因为无法保存前面几次输入的数,后面的结果也没法确定了,而对于n应该让计算机自动处理,不能让用户输入的。主程序是错误的,必须要改变。
添加回答
举报
0/150
提交
取消