Frog jumping Steps (C, Python)

Source: Internet
Author: User

C Language:

1 /*2 -----------------------------------3 when n = 1, there are only 1 hops; when n = 2 o'clock, there are two kinds of jumps; when n = 3 o'clock, there are 3 kinds of jumps; when n = 4 o'clock, there are 5 methods of jumping; when n = 5 o'clock, there are 8 kinds of jumps4 so: Tiaofa (n)5 N=1, Tiaofa (1) = 16 n=2, Tiaofa (2) = 27 n>2, Tiaofa (n) = Tiaofa (n-1) + Tiaofa (n-2)8 -----------------------------------9 */Ten  One# include <stdio.h> A  - intTiaofa (intN) - { the     intI, TF1, TF2, TF3; -TF1 =1; -TF2 =2; -      +     if(n = =1) -TF3 =1; +     Else if(n = =2) ATF3 =2; at     Else -     { -          for(i=3; i<=n; i++) -         { -TF3 = TF1 +TF2; -TF1 =TF2; inTF2 =tf3; -         } to     } +      -     returntf3; the } *  $ intMainvoid)Panax Notoginseng { -     intN; the     Charch; +      Do A     { theprintf"Please enter the number of steps (positive integer):"); +scanf"%d", &n); -printf"%d steps are%d ways to jump. \ n", N, Tiaofa (n)); $printf"\ nyou Want to continue? (y/n):"); $ Flushall (); -scanf"%c", &ch); -} while(ch=='y'|| ch=='Y'); the     return 0;  - }Wuyi  the /* - The output in vc++6.0 is: Wu ----------------------------------- -  About Please enter number of steps (positive integer): 1 $ There are 1 kinds of jumping methods for 1 steps.  - do you want to continue? (y/y): Y - Please enter number of steps (positive integer): 2 - There are 2 kinds of jumping methods for 2 steps.  A do you want to continue? (y/y): Y + Please enter number of steps (Positive integer): 3 the There are 3 kinds of jumping methods for 3 steps.  - do you want to continue? (y/y): Y $ Please enter number of steps (Positive integer): 4 the There are 5 kinds of jumping methods for 4 steps.  the do you want to continue? (y/y): Y the Please enter number of steps (Positive integer): 5 the There are 8 kinds of jumping methods for 5 steps.  - do you want to continue? (y/y): N in Press any key to continue the  the ----------------------------------- About */

Python:

def TF (n):    if n==1:        return 1    elif n==2: return        2    else:        return TF (n-1) +TF (n-2) n = Int (input (' Please enter number of steps (positive integer): ') tf_n = TF (n) print (tf_n)

  

Frog jumping Steps (C, Python)

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.