简体   繁体   English

关于Java伪代码的说明

[英]Clarification on java pseudocode

This was a homework question I had. 这是我的作业问题。

Consider this java pseudocode 考虑一下这个Java伪代码

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? 如果在参数(十进制)637上调用此方法,那么递归调用的前两位数字(十进制)是什么?

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? 看来我可以将637除以8得到79。这是正确的吗?

In your case, the argument is the n . 在您的情况下,参数为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) : 通常,方法调用的参数(或该参数的参数列表)是放在方括号(here)之间的内容:

myMethod(a, b, c);

In this example, a , b , and c are arguments. 在此示例中, abc是参数。 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); 为此,不是行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. 因此,第一个递归调用将具有参数n / 2,这是637/2 = 318的四舍五入部分。反过来,下一个递归调用将是318/2 = 159。

An argument is a parameter passed to a function. 参数是传递给函数的参数。
In your case the function is rep and the argument is n 在您的情况下,函数为rep ,参数为n

First argument is 637. n=637 . 第一个参数是637。n 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) . 它不等于0。不等于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 那将是第一个递归调用,其参数将为318.5,因此答案将为31

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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