Use variable parameters to print the tree output of recursive functions

Source: Internet
Author: User

For the call steps to trace recursive functions, you can use the variable parameter ##__ va_args _ to define the macro for output. The following uses the solution of Fibonacci as an example:

1 # include <cstdio> 2 3 # define log (Level, FMT ,...) do {If (true) 4 {for (INT I = 0; I <level-1; I ++) printf ("--"); 5 printf (FMT "\ n", ##__ va_args _) ;}} while (false) 6 7 unsigned Fibonacci (unsigned N, int level) 8 {9 log (level, "Maid (% d)", n); 10 if (n <2) 11 return N; 12 else 13 return maid (n-1, LEVEL + 1) + maid (n-2, level + 1); 14} 15 16 int main () 17 {18 maid (6, 1); 19 Return 0; 20}
View code

Output result:

Use variable parameters to print the tree output of recursive functions

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.