简体   繁体   中英

Clarification on java pseudocode

This was a homework question I had.

Consider this java pseudocode

string rep (int n){ ---> n is a natural number in this case.

if(n==0)
return "0";
if(n==1)
return "1";
string w = rep(n/2);
if(n%2==0)
  return append (w,'0');
 else
 return append (w,'1');
}

If I call this method on argument (decimal) 637, what is the first two(decimal)-digit argument of a recursive call?

I am confused on what an argument means in this instance. It seems that I can just divide 637 by 8 and get 79. Would this be correct?

In your case, the argument is the n .

In general, the arguments (or argument list for that matter) for a method call is the things which you put between the brackets (here) :

myMethod(a, b, c);

In this example, a , b , and c are arguments. Often people also refer to them as parameters. That's the same in this context.

Now your question goes on to what the first two arguments of the recursive calls are. For this, not that in line string w = rep(n/2); is the recursive call. Thus, the first recursive call will have argument n/2, which is the rounded-down part of 637/2 = 318. In turn, the next recursive call will be 318/2 = 159.

An argument is a parameter passed to a function.
In your case the function is rep and the argument is n

First argument is 637. n=637 . It isn't equal to 0. It isn't equal to 1. w=rep(n/2) => w=rep(637/2) => w=rep(318.5) . That would be the first recursive call, and its argument would be 318.5, so the answer would be 31

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM