recursion - Java Recursive algorithm, I can't follow the logic -


So, the questions ask, what will be the output of this method when you pass it 3.

I plugged it into JCreator and received 010203010 and I have been completely confused.

  Public static zero printX (int n) {if (n & lt; = 0) System.out.print (0); Other {printX (N-1); System.out.print (n); PrintX (n-2); }}   

So this is my reason, it will call 3-1, which will call 2-1, which will call 1-1, which will print zero, Print any one, and then print another zero as 1-2 is at least 0.

So far we have backed up 010 so far for the first time in the "plug-in" recursively.

So this is where it confuses me, it does not really return anything, it just prints, so tells printx (2-1), where hell is its Get value? I can think that it can call itself repeatedly and calls it only in a short time, and the base case reaches 4 or 5 different times.

OK, this argument is:

PrintX (3) -> gt; ; PrintX (3 - 1)

PrintX (2) - & gt; PrintX (2 - 1)

PrintX (1) - & gt; PrintX (1 - 1) that prints ago 0

Then printx (1) calls Print 1 and printx (1 - 2) prints Second 0 .

Then printx calls (2) 2 prints and printx (2 - 2) that prints the third one .

Then printX (3) 3 prints and printx (3 - 2)

printx (1) - & gt; PrintX (1 - 1) that prints fourth 0 .

Then printX (1) Print 1 and printX (1 - 2) << Strong> 010203010 <



Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -