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.

# Include <stdio. h> // print 2 * n-1 rows in total, and print 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 * digits 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.

# 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);} // recursive version for removing repeated calculation of int F2 (int n) {// Save the array static result of the intermediate result [Max] = }; 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];} // 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) = % d \ n", F1 (40); End = clock (); printf ("Time: % d Ms \ n ", end-Start); Start = clock (); printf (" F (40) = % d \ n ", F2 (40 )); end = clock (); printf ("time used: % d Ms \ n", end-Start); Start = clock (); printf ("F (20) = % d \ n ", F3 (20); End = clock (); printf (" time used: % d Ms \ n ", end-Start ); start = clock (); printf ("F (20) = % d \ n", F4 (20); End = clock (); printf ("Time: % d Ms \ n ", 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.