Welcome Guest.

1Answers

How does the loop in this recursive function work?

Asked by: Mark Moore 186 views IT August 17, 2019

#include<cstdio>

using namespace std;

             int n,cnt=1;

            void func(int x)

            {    

                   for(int i=1;i<=x/2;i++)

                    {        cnt++;        func(i);    } 

            }

           int main()

           {   

                   scanf("%d",&n );    

                  func(n);    

                  printf("%d\n",cnt); 

           }

Addition: Is it a time to accumulate counts when i=1, 2, 3?

Addition: The input data is a question on the 6th valley. .

1 Answers

  1. +7Votes  

    That’s for sure, cnt is a global variable, as long as it is executed cyclically, it will add 1 on the previous basis, regardless of how many times the function runs

    Sharon Robinson- August 17, 2019 |