Zookeeper
1.Implement with template Meta50One or two steps at a time3Step
2.Analysis
The above analysis shows thatN(N> 3) The number of times when the level step is: before the target step3The total number of times each step goes directly to the target step.
3.The template meta optimizes the time consumed during runtime in the compiler key.
4.The Code implemented using the template metadata is as follows:
# Include <iostream>/* Here is int type, N indicates the number of steps */template <int n> struct data {Enum {res = data <n-1> :: res + Data <n-2 >:: res + Data <n-3 >:: res };}; template <> struct data <1 >{ Enum {res = 1 };}; template <> struct data <2 >{ Enum {res = 2 };}; template <> struct data <3 >{ Enum {res = 4 };}; void main () {const int Myint = 4; int num = data <Myint >:: res; // <> internal variables STD: cout <num <STD: Endl; STD: cin. get ();}
The running result is:7
2.Use the template element to calculate the number of orders
/* Use the template element to calculate the number series of the pona subscribe, such as, 5 ,...., where F (n) = f (n-1) + f (n + 2) */# include <iostream>/* is of the int type, N indicates the number of steps */template <int n> struct data {Enum {res = data <n-1 >:: res + Data <n-2> :: res };}; template <> struct data <1 >{ Enum {res = 1 };}; template <> struct data <2 >{ Enum {res = 1 };}; void main () {const int Myint = 5; int num = data <Myint >:: res; // <> internal variables STD: cout <num <STD: Endl; STD: cin. get ();}: 5
Zookeeper