Introduction to several solutions for printing diamond and Fibonacci sequences

Source: Internet
Author: User
Tags printf

1, write the program, printing * Diamond
the number of blank spaces and * numbers to be printed in line I, printing each line in the For loop

Copy Code code as follows:


#include <stdio.h>


///Total print 2*n-1 line, print line by row


void print1 (int n)


{


int i,j;


for (i=1;i<=n;i++) {//print 1 to n rows


for (j=1;j<=n-i;j++)//print n-i spaces


printf ("");


for (j=1;j<=2*i-1;j++)//Print 2*i-1 * Number


printf ("*");


printf ("n");


}


for (; i<2*n;i++) {//print n+1 to 2*n-1 row, same (2*n-i) row


for (j=1;j<=n-(2*n-i); j + +)


printf ("");


for (j=1;j<=2* (2*n-i) -1;j++)


printf ("*");


printf ("n");


}


}


void Main ()


{


int n;//n is the number of * numbers on a diamond edge


printf ("Enter N:");


scanf ("%d", &n);


print1 (n);


}


2, the Fibonacci sequence (Fibonacci Sequence), also known as the Golden Division Series, refers to such a series:
1, 1, 2, 3, 5, 8, 13, 、...... Mathematically, the Fibonacci sequence is defined as the following recursive method: F (0) =0,f (1) =1,f (n) =f (n-1) +f (n-2) (n>=2,n∈n*). Write a program that outputs the value of F (20).

Here are four different solutions, noting that there is a big difference between recursion and improved recursive efficiency.

Copy Code code as follows:


#include <stdio.h>


#include <math.h>


#include <time.h>


#define MAX 100


//Recursive


int F1 (int n)


{


if (n==1 | | n==0)


return 1;


return F1 (n-1) +f1 (n-2);


}


//improved recursion, removal of repetitive computations


int F2 (int n)


{


//Save an array of intermediate results


Static result[max]={1,1};


if (n==1 | | n==0)


return 1;


if (result[n-1] = = 0)


result[n-1]=f2 (n-1);


if (result[n-2] = = 0)


RESULT[N-2]=F2 (n-2);


return result[n-1]+result[n-2];


}


//Save intermediate Results with array (from Chen Xiaojie)


int f3 (int n)


{


int a[max],i;


a[1]=1;


a[0]=1;


for (i=2;i<=n;i++)


A[i]=a[i-1]+a[i-2];


return a[n];


}


//Iterative


int f4 (int n)


{


int i=2,a=1,b=1,sum=1;


while (i<=n) {


sum=a+b;


a=b;


b=sum;


i++;


}


return sum;


}


void Main ()


{


long start,end;


Start=clock ();


printf ("F () ==%dn", F1 (40));


End=clock ();


printf ("Spents:%d MSN", End-start);


Start=clock ();


printf ("F () ==%dn", F2 (40));


End=clock ();


printf ("Spents:%d MSN", End-start);


Start=clock ();


printf ("f) ==%dn", F3 (20));


End=clock ();


printf ("Spents:%d MSN", End-start);


Start=clock ();


printf ("f) ==%dn", F4 (20));


End=clock ();


printf ("Spents:%d MSN", End-start);


}


Run Result:

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.