Java:递归打印钻石

时间:2011-02-26 23:31:31

标签: java recursion

如果只给出尺寸,你将如何使用Java递归打印钻石?

大小为5会产生:

 ***** *****
 ****   ****
 ***     ***
 **       **
 *         *

 *         *
 **       **
 ***     ***
 ****   ****
 ***** *****

我到目前为止的代码

public static void dia(int statSize, int size,int count) {

      int statSizeLarge = (statSize*2)+1; 

      // Params:
      // statSize == static size, never change this
      // size == variable size, change this
      // count == counter

      if(size==0) {
              System.out.println(); 
      } else {

          // is the counter smaller then the size
          // if yes, increment and keep printing
          if(count<size){
              System.out.print("*");
          } 



          // is greater then size? 
          // if yes, move on, print 
          // a few more stars
              if((count<=statSizeLarge)){
                  if(count<statSize+1 && (count>size)){
                      System.out.print(" ");
                  }else if (count>size+1){
                      System.out.print("*");
                  } else {}
                  dia(statSize,size,count+1);
              }



         // reset count, move to next element
          if(count>=statSizeLarge) {
              count = 0; 
              System.out.println();
              dia(statSize,size-1,count);
          }



      } // ends Else  

  }

OutPut:

Enter commands:
diamond 3
******
** ****
*  ****




*  ****




** ****
*  ****




*  ****

3 个答案:

答案 0 :(得分:6)

要创建更大的钻石,请选择较小的钻石并添加两个额外的行和列。在下面的diagrom中,为了清晰起见,我用点替换空格。在第二颗钻石中,新添加的字符以粗体显示。

              *****.*****  <-- extra row
****.****     ****...****
***...***     ***.....***
**.....**     **.......**
*.......*     *.........*
......... --> ...........
*.......*     *.........*
**.....**     **.......**
***...***     ***.....***
****.****     ****...****
              *****.*****  <-- extra row
                   ^^
                   ||
                   extra columns

你的递归函数应该打印第一行,然后打印一个较小的钻石,中间有两个额外的列,然后是最后一行。

在伪代码中:

void diamond(stars, spaces) {
    if (n == 0) {
        print(' ' * spaces)
    } else {
        print('*' * stars, ' ' * spaces, '*' * stars)
        diamond(stars - 1, spaces + 2)
        print('*' * stars, ' ' * spaces, '*' * stars)
    }
}

由于这是一个学习练习,我不会给你完整的Java源代码 - 你可以自己编写它。在这里,您可以看到它在Python中在线运行,这样您就可以看到该算法有效:

答案 1 :(得分:1)

提示:在输出中查找模式。尝试将该模式映射到递归调用,其中方法执行某些操作,调用自身,然后执行其他操作。

答案 2 :(得分:0)

这是打印明星钻石的Java程序:

class DiamondPattern
{
  static public int ReadInteger()
  {
        try
        {
              String inpString = "";
              InputStreamReader input = new InputStreamReader(System.in);
              BufferedReader reader = new BufferedReader(input);
              String s = reader.readLine();
              return Integer.parseInt(s);
        }
        catch (Exception e)
        {
              e.printStackTrace();
        }
        return -1;
  }

  public static void main(String[] args)
  {
        System.out.println("Program for displaying pattern of *.");
        System.out.print("Enter the maximum number of *: ");
        int n = ReadInteger();

        System.out.println("\nHere is the Diamond of Stars\n");

        for (int i = 1; i <= n; i++)
        {
              for (int j = 0; j < (n - i); j++)
                    System.out.print(" ");
              for (int j = 1; j <= i; j++)
                    System.out.print("*");
              for (int k = 1; k < i; k++)
                    System.out.print("*");
              System.out.println();
        }

        for (int i = n - 1; i >= 1; i--)
        {
              for (int j = 0; j < (n - i); j++)
                    System.out.print(" ");
              for (int j = 1; j <= i; j++)
                    System.out.print("*");
              for (int k = 1; k < i; k++)
                    System.out.print("*");
              System.out.println();
        }

        System.out.println();
  }
}
相关问题