如何将此循环转换为递归?

时间:2014-08-04 13:39:11

标签: java recursion

I'm trying to solve this problem but in stuck in converting my while loop into recursion
我设法实现了printMany函数,如下所示

public static void printMany(int count, String s){  
  if(count >= 1) {
    System.out.print(s);
    printMany(count-1, s);
  }
}   

但是沙漏方法的当前实现仍然使用循环,尽管它显示正确的输出。

public static void hourglass(int numberOfStars, int numberOfSpaces){
  while(numberOfStars>0){
    printMany(numberOfSpaces++, " ");
    printMany(numberOfStars--, "X ");
    System.out.println();
  }  

  numberOfSpaces -=2;;
  numberOfStars += 2;

  while(numberOfSpaces>=0){
    printMany(numberOfSpaces--, " ");
    printMany(numberOfStars++, "X ");
    System.out.println(); 
  }
}  

我想问一下,如何将此循环转换为递归调用?

5 个答案:

答案 0 :(得分:1)

我不仅会给你答案,而且我会尽力帮助你。如果你想使用递归和没有循环来解决这个问题,关键是要弄清楚递归辅助函数的参数必须是什么。看起来你总是需要记住原始用户输入(知道要打印多少空格并知道何时停止递归),你当前的星数,以及你是否'重新放在金字塔的上半部分或下半部分。鉴于所有这些信息,您应该能够做两件事。首先,您应该能够正确地打印出一条线。其次,您应该能够确定下一行应该是什么。鉴于此,您可以打印并递归,一旦达到基本案例就停止。

答案 1 :(得分:0)

这是一种可能的解决方案:

public static void printMany(int count, String s) {
    if (count == 0)
        return;

    System.out.print(s);
    printMany(count - 1, s);
}

public static void upperhalf(int count, int max) {
    if (count == 0)
        return;

    printMany(max - count, " ");
    printMany(count, "* ");

    System.out.println();

    upperhalf(count - 1, max);
}

public static void lowerhalf(int count, int max) {
    if (count == max)
        return;

    printMany(max - count - 1, " ");
    printMany(count + 1, "* ");

    System.out.println();

    lowerhalf(count + 1, max);
}

public static void hourglass(int n) {
    upperhalf(n, n);
    lowerhalf(0, n);
}

在例如{}内拨打hourglass(1); main结果为:

*
*

所以hourglass(2);打印:

* * 
 *
 *
* * 

等等......

答案 2 :(得分:0)

您可以调用相同的函数来代替循环程序。

public static void hourglass(int numberOfStars, int numberOfSpaces){
    hourglass(numberOfStars, numberOfSpaces, false);
}

private static void hourglass(int numberOfStars, int numberOfSpaces, boolean dir){
    if(dir==false && numberOfStars > 0){
        printMany(numberOfSpaces, " ");
        printMany(numberOfStars, "X ");
        System.out.println();
        hourglass(--numberOfStars, ++numberOfSpaces, false);
        return;
    }

    if(numberOfStars==0){
        numberOfSpaces -=2;
        numberOfStars += 2;
    }

    if(numberOfSpaces>0){
        printMany(numberOfSpaces, " ");
        printMany(numberOfStars, "X ");
        System.out.println();
        hourglass(++numberOfStars, --numberOfSpaces, true);
    }
}

答案 3 :(得分:0)

这里有一段代码供您使用。使用对称填充。

public class Hourglass {

    public static void printRow(int nStars, int padding) {
        if (nStars == 0)
            return;

        if (padding > 0) {
            System.out.print(" ");
            printRow(nStars, padding - 1);
            System.out.print(" ");
            return;
        } 

        if (nStars <= 0) 
            return;

        System.out.print("*");
        if (nStars > 1) {
            System.out.print(" ");
            printRow(nStars - 1, padding);
        }
    }

    public static void printTop (int height, int padding) {
        if (height == 0)
            return;

        printRow(height,padding);
        System.out.print("\n");
        printTop(height - 1, padding + 1);
    }

    public static void printBottom(int currentHeight, int height, int padding) {
        printRow(currentHeight, padding);
        System.out.print("\n");
        if (currentHeight < height)
            printBottom(currentHeight + 1, height, padding - 1);
    }

    public static void printHourglass(int height) {
        if (height <= 0)
            throw new IllegalArgumentException();

        printTop(height, 0);
        printBottom(1, height, height - 1);
    }

    public static void main(String[] args) {
        printHourglass(5);
    }
}

答案 4 :(得分:0)

将该方法拆分为两个,这是针对您的问题的另一个递归解决方案:

public static void hourglass(int numberOfStars, int numberOfSpaces) {
    if(numberOfStars== 0) return;

    printMany(numberOfSpaces++, " ");
     printMany(numberOfStars--, "X ");
     System.out.println();
     hourglass(numberOfStars,numberOfSpaces);  
     numberOfSpaces -=2;
     numberOfStars += 2;
         if(numberOfStars==2)
     hourglassBottom(numberOfStars,numberOfSpaces);

}
public static void hourglassBottom(int numberOfStars, int numberOfSpaces){
    if(numberOfSpaces==0 )return;

     printMany(numberOfSpaces--, " ");
        printMany(numberOfStars++, "X ");
        System.out.println(); 
        hourglassBottom(numberOfStars,numberOfSpaces);  

}

例如,运行沙漏(3,1);会给你以下的信息:

 X X X 
  X X 
   X 
  X X 
 X X X