有人可以帮我理解这两段代码[递归]吗?

时间:2016-03-04 05:56:21

标签: java recursion worksheet

我正在努力了解此代码中发生了什么。在我们为家庭作业提出的14个问题中,这些是我遇到的唯一2个问题而且只是在不知道他们如何得到答案的情况下放下答案。任何帮助将不胜感激,谢谢。

这是#1:

public void printDollarSign(int k)
{
        int j;
        if (k>0)
        {
              for (j=1; j<= k; j++)
                  System.out.print("$");
              System.out.println();
              printDollarSign(k-1);
        }
}

如果呼叫是:printDollarSign(5); ?

答案是:

$$$$$

$ $ $ $

$$$

$$

$

这是#2:

public void bbb(String s, int p)
{
    if (p>= 0)
    { 
        bbb(s,p-1);
        System.out.print(s.charAt(p)); 
    }
}

如果呼叫是:bbb(“January”,4),输出结果如何; ?

答案是:

Janua

6 个答案:

答案 0 :(得分:0)

解释 printDollarSign

  • 这里创建了一个递归函数。
  • 每个递归函数都有一个基本条件。在你的情况下,它的if(k> 0)。
  • 每次递归调用都会将每次递归调用的k值减1.Hence,for循环运行的次数减少1次。

对于第一个电话:

  1. 假设k = 5;
  2. 函数调用printDollarSign(5);
  3. 检查是否(k> 0),即5> 0;
  4. for循环运行5次并打印5&#34; $&#34;标志;
  5. println打印换行符;
  6. 递归调用printDollarSign(4);

答案 1 :(得分:0)

printDollarSign(k)首先打印k个$,然后打印换行,然后先打印printDollarSign(k-1)打印k-1个$,然后打印换行然后再打电话printDollarSign(k-1-1)......这一直持续到k = 0。当k = 0时,printDollarSign(0)不打印任何内容。

答案 2 :(得分:0)

你的第一个是创建一个带有int特定参数的函数,在这种情况下&#34; k&#34;。

  • 然后通过&#34; int k;&#34;
  • 设置一个名为j的int
  • 然后检查k是否大于0
  • 检查后,它循环通过k(其中j = 1且j
  • 它将产生&#34; $$$$$&#34; ..&#34; $$$$&#34; ..等等。直到k < 0
  • 然后写一个新行,然后再次使用k
  • 中的1来调用该函数

代码:

public void printDollarSign(int k){ //Define function
        int j;//Define j as an int
        if (k>0){//Check if k is greater than 0
             for (j=1; j<= k; j++)//Loop through k where j = 1
                 System.out.print("$");//Print $ by the amount of k
             System.out.println();//Print a new line
             printDollarSign(k-1);//Re run the function
        }
}

你的第二个问题是创建一个带有两个参数string和int&#34; s&#34;和&#34; p&#34;

  • 检查p是否大于0
  • 然后调用本身内部的函数,从p(p-1)
  • 中删除1
  • 然后根据p
  • 从字符串中打印出一个字符

代码:

public void bbb(String s, int p){//Define Function
    if (p>= 0){ //Check if p is greater than 0
        bbb(s,p-1);//Rerun function
        System.out.print(s.charAt(p));//Print character of string based on p
    }
}

答案 3 :(得分:0)

添加代码注释以供解释:

1#

public void printDollarSign(int k)
    {
        // temporary variable j, it will be always initialized to 1 inside the for loop.
        int j;
        // only executed to be true if k is more than 0, that means if K is initially 5
        // then it only works for 5,4,3,2,1  and not for 0.
        if (k>0)
        {
            // always starts with 1 and goes to the value of k, that means if K is currently 5
            // then it will print 5 dollars, if 4 then 4 dollars and so on
            for (j=1; j<= k; j++)
                System.out.print("$");
            // a new line will be added once dollars are printed.
            System.out.println();
            // this will again call the same function and decrements the value of k, so next time 
            // k will have the value one less then the previous one, if this has printed 5 dollars 
            // in last iteration next time it will print 4 dollars and so on
            printDollarSign(k-1);
        }
    }

2#

public void bbb(String s, int p)
    {
        // only print a character if p has a value grater than 0. in you case , p has a value 4 that
        // mean only 4 characters will be printed at max
        if (p>= 0)
        { 
            // recuresively call same method by decrementing p, so it will be
            // [bbb(s,3) prints 'a']-> [bbb(s,3) prints 'u']-> bbb(s,2) [prints 'n']-> [bbb(s, 1) prints 'a']> [bbb(s, 0) prints 'J']
            // last character will be printed first
            bbb(s,p-1);
            // prints the character at p location
            System.out.print(s.charAt(p)); 
        }
    }

答案 4 :(得分:0)

bbb 的说明:

  • 它是一个递归应用程序,用于从给定字符串中提取子字符串。
  • 基础条件if(p> = 0)。
  • 你应该知道,对于每一次递归都要调用&#34; P&#34;存储在堆栈中。
  • 如您所知堆栈是后进先出(LIFO)数据结构,将首先弹出插入堆栈的最后一个值。

执行:

  1. bbb(&#34; January&#34;,4); - &GT;堆栈包含: [4]
  2. bbb(&#34; January&#34;,3); - &GT;堆栈包含: [3 4]
  3. bbb(&#34; January&#34;,2); - &GT;堆栈包含: [2 3 4]
  4. bbb(&#34; January&#34;,1); - &GT;堆栈包含: [1 2 3 4]
  5. bbb(&#34; January&#34;,0); - &GT;堆栈包含: [0 1 2 3 4]
  6. 现在弹出每个元素并在该位置打印char

    1. 0之后 - > Ĵ
    2. 1之后 - > JA
    3. 2之后 - >扬
    4. 3 - >之后亚努
    5. 4 - >之后Janua

答案 5 :(得分:0)

if you provide a string s as 'January', the way it stores is like this:
0th position = J, 
1th position = a,
2th position = n,
3th position = u,
4th position = a,
5th position = r,
6th position = y,
Just like an array of characters. 
When you provide p=4, you are setting the closing criteria for the condition    check. 

function bbb('January', 4){
if(4>=0){
bbb('January', 3); ....

function bbb('January', 3){
if(3>=0){
bbb('January', 2); ....

function bbb('January', 2){
if(2>=0){
bbb('January', 1); ....

function bbb('January', 1){
if(1>=0){
bbb('January', 0); ....

function bbb('January', 0){
if(0>=0){
bbb('January', -1); ....

function bbb('January', -1){
if(-1>=0){ Here condition check fails.. hence char at(-1) doec not print

return goes back to print of previous call and prints J as p=0, we have J
the p=1: a
p=2: n
p=3: u
p=4: a

和功能完成......

如果解释有用,请告诉我