一本通1165 Hermite函数(递归)

【题目描述】

用递归的方法求Hermite多项式的值

                        1                            n=0                                                         

hn(x)={    2x                          n=1

               2xhn-1(x)-2(n-1)hn-2(x)        n>1

对给定的x和正整数n,求多项式的值。

【输入】

给定的n和正整数x。

【输出】

多项式的值。

【输入样例】

1 2

【输出样例】

4.00

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
double h(int n,int x)//这里是重点,因为题目中并没有说得到的结果一定是整数,所以我一开始写:int h(int n,int x)是不对的
{
if(n==0)
return 1;
if(n==1)
return 2*x;
if(n>1)
return 2*x*h(n-1,x)-2*(n-1)*h(n-2,x);
}
using namespace std;
int main()
{
int n,x;
double m;
cin>>n>>x;
m=h(n,x);
printf("%.2lf",m);
return 0;
}

原文地址:https://www.cnblogs.com/57xmz/p/12250271.html