Introduction to several solutions for printing diamond and Fibonacci Series

Source: Internet
Author: User

1. Write a program and print * Diamond
Releases the number of spaces to be printed on line I and the number of numbers *, and prints the rows in sequence using the for loop.

Copy codeThe Code is as follows:
# Include <stdio. h>
// Print 2 * n-1 rows in total and print rows 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 * Numbers
Printf ("*");
Printf ("n ");
}
For (; I <2 * n; I ++) {// print n + 1 to 2 * n-1 rows, same as (2 * n-I) rows
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 * digits on the side of the Diamond
Printf ("enter n :");
Scanf ("% d", & n );
Print1 (n );
}


2. the Fibonacci Sequence, also known as the Golden series, refers to a series like this:
1, 1, 2, 3, 5, 8, 13, 21 ,...... In mathematics, the Fibonacci sequence is defined as follows in a recursive method: F (0) = 0, F (1) = 1, F (n) = F (n-1) + F (n-2) (n> = 2, n *). write a program and output the value of F (20.

Here we provide four different solutions. Note that there is a big difference between Recursion and improved recursion efficiency.

Copy codeThe Code is as follows:
# Include <stdio. h>
# Include <math. h>
# Include <time. h>
# Deprecision MAX 100
// Recursion
Int f1 (int n)
{
If (n = 1 | n = 0)
Return 1;
Return f1 (n-1) + f1 (n-2 );
}
// Recursive version to remove repeated computations
Int f2 (int n)
{
// Save the 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 (The N-2 );
Return result [n-1] + result [N-2];
}
// Use an array to save the intermediate result (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];
}
// Iteration
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 (40) = % dn", f1 (40 ));
End = clock ();
Printf ("time used: % d msn", end-start );
Start = clock ();
Printf ("f (40) = % dn", f2 (40 ));
End = clock ();
Printf ("time used: % d msn", end-start );
Start = clock ();
Printf ("f (20) = % dn", f3 (20 ));
End = clock ();
Printf ("time used: % d msn", end-start );
Start = clock ();
Printf ("f (20) = % dn", f4 (20 ));
End = clock ();
Printf ("time used: % d msn", end-start );
}


Running 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.