Simple Fibonacci--garlic (4)

Source: Internet
Author: User

The title is from the 4th question of "garlic guest".

Before solving the algorithm problem, we must first write the corresponding mathematical expression, which is used to describe the algorithm.

Mathematical description

According to the "Mathematical description", write the code as follows:

#include <stdio.h>intMain () {intN =0 ; scanf ("%d", &N); intI, fn1 =1, fn2 =0, FN; Switch(N) { Case 0: printf ("0");  Break;  Case 1: printf ("1");  Break; default:             for(i =2; I <= N; i++) {fn= Fn1 +fn2; FN2=fn1; FN1=fn; } printf ("%d", FN); }    return 0;}

Happy to finish.

Reflection, the time complexity of the algorithm is O (N), that is, the execution time of the algorithm depends on the number of items in the sequence. If you can push the "nth Xiang Feipo formula" and bring it directly into the equation, then the time complexity will evolve to O (1).

Simple Fibonacci--garlic (4)

Related Article

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.