輸入一個正整數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
提交
取消