//// main.cpp// 3-13递归Fibonacci级数//// Created by T.P on 2018/3/21.// Copyright ? 2018年 T.P. All rights reserved.////课本习题3-13.用递归的方法编写函数求Fibonacci级数,//公式为:Fn=Fn-1 + Fn-2 (n>2),F1=F2=1#include <iostream>using namespace std;int fibon(int n,int &s){ if (n==2||n==1) s=1; //注意递归归,验证,从最小的地方推 else s=fibon(n-1,s)+fibon(n-2,s); return s;}int main(){ int s=0; int n; cout<<"请输入Fibonacci级数的n值(n>2):"; cin>>n; fibon(n,s); cout<<s<<endl; return 0;}
//// main.cpp// 3-14递归n阶勒让德多项式//// Created by T.P on 2018/3/21.// Copyright ? 2018年 T.P. All rights reserved.////课本习题3-14//Pn(x)=1, n=0// =x, n=1// =[(2n-1)x*Pn-1(x)-(n-1)Pn-2(x)]/n ,n>1#include <iostream>using namespace std;int LRD(int x,int n){ if(n==0) return 1; else if (n==1) return x; else return ((2*n-1)*x*LRD(x, n-1)-(n-1)*LRD(x, n-2))/n;}int main(){ int x,n; int s=0; cin>>x>>n; s=LRD(x, n); cout<<s<<endl; return 0;}
"C + +" recursion after class exercises 3-13, 3-14