《C程序设计》(夏宝岚)
勒让德多项式定义为:
┌ 1 (n=0)
n!=ㄧ x (n=1)
└ ((2*n-1)*p(n-1,x)-(n-1)*p(n-2,x))/n (n>1)
#include <stdio.h>
double p(int n, double x)
{
if(n == 0)
return 1;
else
if(n == 1)
return x;
else
return ((2*n-1) * p(n-1, x) - (n-1) * p(n-2, x)) / n;
}
int main()
{
int n;
double x;
double y;
printf("input n, x:\n");
scanf("%d %lf", &n, &x);
y = p(n, x);
printf("%-10.0f\n", y);
return 0;
}
运行结果(VC):
==========================
input n, x:
4 5.6↙
11
==========================
评论