Use the template element to implement 50 steps, one or two or three steps at a time, and use the template element to calculate the sequence of orders

Source: Internet
Author: User

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

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.