C-Language algorithm: Perfecting the K-order Fibonacci sequence of the year

Source: Internet
Author: User

The following is the log replay for the sophomore time:
"The topic extends to the K -order,

kThe order Fibonacci sequence, 1 order (i.e.k=1):1,1,1,1,1,1,1、......

a0=a[1-1]=1,a1=1,a2=1,a3=1,a4=1,a5=1,a6=1 ...

3Order (k=3):0,0,1,1,2,4,7、、、、、

A0=0,a1=0,a2=a[3-1]=1,a3=0+0+1=1,a4=0+1+1=2,a5=1+2+4=7

4Order:0,0,0,1,1,2,4,8, the, -...

a0=0,a1=0,a2=0,a3=a[4-1]=1,a4=1,a5=2,a6=4 ... a[8]=1+2+4+8=15 ...

The problem is generalized and it can be seen that:

k-2 0 ;

Section k-1 the value of the item is 1 ;

Sectionkto the2k-1the value of the item is2of theNThe Second party (also the formerkitems and But at the same time with2of theNThe same as the second-party value);

Section 2k The value after the item is the previous k items and;

Web-based method of ' recursion ', and only correspondence 2 step, not extended to K step, the younger brother also skilled ' recursion ', or continue to use the order method.

c

Status fibonacci (int k, int m, int &f)/*  the value of item m of the K-order Fibonacci sequence f     */{ int a[100],i,y,z=0;     /* defines an array with an element number of 100. */ if (k<=1| | M&LT;0) return error;    /* returns an error warning */ else if (m<k-1) If the input parameter is not canonical f=0;       /* constant equals data segment processing */ else if (m==k-1) f=1; else if (m<=2*k-1) F=pow ( 2,M-K);     /* two-segment processing */ else                   /*k stage processing, time complexity */ { for (y=0;y<k;y++)        /* assigns a value */ a[y]=pow (2,y) to the K array element with a for loop;  for (i=2*k;i<=m;i++)  { &NBSP;A[K]=0;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;/*A[K] */ for (y=0;y<k;y++)       /* embedded in the For loop body inside the For loop body */ { a[k] Each time the loop is cleared 01 times =a[k]+a[y];        /* K-Item plus */ }   a[z]=a[k];   /* Each move to the value of the smallest element of the eradication, change to the maximum value, do not have to assign a value, save a large amount of time, again added "output A[K] value" can achieve the first m-k and output */  z++;       /*z each time you add 1, make sure to overwrite the smallest item with the largest item of the top K item */  if  (z==k) z=0;  /* when z==k, Z zeroing */  }   f=a[k];      /* output: The value of item m of the K-order Fibonacci sequence number F */ } return  ok;}


The following is an optimized algorithm that has a faster processing time in the Obline judge.

Status fibonacci (int k, int m, int &f)/*  the value of item m of the K-order Fibonacci sequence f     */{ long a[300], i, y, z=0;                     /* defines an array with an element number of 300, The limit in long is less than the No. 300 number. */ if (k <= 1| | M&NBSP;&LT;&NBSP;0) return error;    /* returns an error warning */&NBSP;ELSE&NBSP;IF if the input parameter is not canonical (m  &LT;&NBSP;K-1) f=0;                        /* constant equals data segment processing */ else if (m == k-1) f=1;  Else for (i=0; i<k; i++) a[i]=0;  a[k]=1;                              /* input k 0 and A[k]=1 initial digital */ for (y=k+1; y<=m; y++)  a[y]=a[y-1]*2-a[y-k];          /* Law: The number of y is equal to y-1, multiplied by 2 minus the y-k number */   f=a[y-1];                             /* output: The value of item m of the K-order Fibonacci sequence number f */  } return ok;}


This article is from the "zgysolution" blog, make sure to keep this source http://zgysolution.blog.51cto.com/9605745/1582596

C-Language algorithm: Perfecting the K-order Fibonacci sequence of the year

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.