Recursive recursive practice--k

Source: Internet
Author: User

Title Requirements:

Enhanced Fibonacci sequence: f[i] = i (i <= 3); F[i] = F[i-1] + f[i-2] + f[i-3] (i >= 4), Input n (1<=n&&n<=30), output F[N]. Topic idea: Define array A, give directly a[1]=1, a[2]=2, a[3]=3, define Loop Loop 30 times f[1] to f[30], enter the value of N, direct output F[N]. Detail handling: Because the value of n is very small, the value of all F[n] is calculated directly, while the while function is used to output f[n when the value of n is entered, which prevents time-outs.

#include <bits/stdc++.h>
using namespace std;
int main ()
{    long  a[35];
      int i,n;
    a[1]=1;a[2]=2;a[3]=3;
      for (i=4;i<=30;i++)
    a[i]=a[i-1]+a[i-2]+a[i-3];
    while (Cin>>n)
   {   cout<<a[n]<<endl;
 }
 return 0;
}

Sentiment: For a good understanding of the problem, to pay attention to the details of the treatment, to prevent data overflow, too many operations, timeouts and other problems.

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.