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)