用递归函数计算厄密多项式

《C和指针》第7章第1道编程题:

Hermite Polynomials(厄密多项式)是这样定义的:

 

例如,H3(2)的值是40。请编写一个递归函数,计算Hn(x)的值。函数原型为:

int hermite( int n, int x );

 

 1 /*
 2 ** 计算Hermite Polynomials(厄密多项式)的值
 3 */
 4 
 5 #include <stdio.h>
 6 
 7 int hermite( int n, int x );
 8 
 9 int 
10 main()
11 {
12     int n, x;
13     scanf( "%d%d", &n, &x );
14     printf( "%d", hermite( n, x ) );
15     return 0;
16 }
17 
18 /*
19 ** 计算厄密多项式的值,递归函数版本
20 */
21 int 
22 hermite( int n, int x )
23 {
24     int result;
25     
26     if( n <= 0 )
27         result = 1;
28     else {
29         if( n == 1 )
30             result = 2 * x;
31         else
32             result = 2 * x * hermite( n - 1, x ) 
33                 - 2 * ( n - 1 ) * hermite( n - 2, x );
34     }        
35     return result;
36 }

 

posted on 2014-11-21 21:30  丝工木每  阅读(1404)  评论(0编辑  收藏  举报

导航