"C + +" recursion after class exercises 3-13, 3-14

Source: Internet
Author: User

////  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.