【问题标题】:Mirrored Triangle generation in javajava中的镜像三角形生成
【发布时间】:2017-01-17 18:43:52
【问题描述】:

我碰巧出现了一个测试,并得到以下问题。我无法弄清楚如何进行。场景是编写一个java程序,用相应的N打印以下内容。如果假设N=3,它必须有2*N行,输出必须是,

1
2*3
4*5*6
4*5*6
2*3
1

输出必须仅包含数字和星号。 N 在 0 到 100 之间变化。另外,给定

public static void main(String[] args){
    int rows=2;
    mirrorTriangle(rows);
}
public void mirrorTriangle(int n){
    //Logic 
}

如果行应该随 N 变化,我不明白为什么将行声明为 2。请解释逻辑。

【问题讨论】:

  • 家庭作业??你写的代码有什么问题吗?
  • 在面试过程中发现了这个。我不知道继续。
  • 如果given 意味着您只能在//Logic 所在的位置编写代码,那么您误解/记错了一些东西。这是没有意义的(而且问题是不可能的)。确切的问题表述可能会有所帮助。如果这只是代码结构的一个示例 - 是的,答案将看起来像这样,无需解释。
  • 我认为您需要研究递归并维护递归索引。那么事情可能会为你澄清。
  • 这是给定和问的:(

标签: java loops


【解决方案1】:

请找到您的问题的解决方案,并附上解释。

public static void main(String[] args) throws Exception
    {
        // initialize n
        int n = 4;
        // initialize x to 1 from where our printing will start.
        int x = 1;
        /* We will store our generated numbers in an array.
         * For example, the array after we generate 
         * the numbers would look like:
         * [1,0,0,
            2,3,0,
            4,5,6,
            4,5,6,
            2,3,0,
            1,0,0]
         * 
         * When n = 3, there are going to be 3*2 i.e, n*2 rows.
         * in our case 6 rows. 
         * visualize with the above values.
         * The first n/2 rows will be the numbers we print, 
         * the next n/2 will be the mirror image of the first n/2 rows.
         * no. of columns in each row will be equal to n, in our example:3
         */
        int arr[][] = new int[n*2][n];
        /*
         * Start populating the matrix
         * Each row will contain number of elements eaual to the row number,
         *  so 1st row -> 1 element, 2nd - > 2,.. and so on.
         */
        for(int row=0;row<n;row++)
        {
            int col = 0;
            while(col < row+1)
            {
                arr[row][col] = arr[n*2-row-1][col] = x++;
                col++;
            }
        }
        /*
         * Now our task is just to read out the array.
         * The tricky part is adding the astricks.
         * We notice that row1 will have 1-1 asticks, row2 -> 2-1 astricks ,.. and so on.
         * So in between the numbers while reading out,
         * for each row we maintain the number of astricks.
         */
        for(int i=0;i<arr.length;i++)
        {
            StringBuilder build = new StringBuilder();
            for(int j=0;j<arr[i].length;j++)
            {
                if(arr[i][j] > 0)
                {
                    build.append((arr[i][j])).append("*");
                }
            }
            System.out.print(build.delete(build.length()-1,build.length()).toString());
            System.out.println();
        }
    }

n=4 的o:p:

1
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
1

【讨论】:

    【解决方案2】:
    def N = 3
    def i = 0
    def j = 0
    int[][] numbers = new int[N][]
    
    // Generate, print, and store numbers
    while( i < numbers.length ){
        numbers[i] = new int[i+1]
        j = 0
        while( j < numbers[i].length ){
            numbers[i][j] = j+1
            ++j
            print j
        }
        println ""
        i++
    }
    
    // Print them again, in reverse order
    i = numbers.length - 1
    while( i >= 0 ){
        j = 0
        while( j < numbers[i].length ){
            print numbers[i][j]
            j++
        }
        println ""
        i--
    }  
    

    输出:

    1
    12
    123
    123
    12
    1
    

    代码是不言自明的。您只需要 N 行但打印 2N 因为,等待它......对称。如果您有 6 行,前 3 行是新的,而其他 3 行只是镜像,那么当您可以再次打印它们时,为什么还要浪费内存空间呢?

    【讨论】:

    • @BatScream 尖叫声 dafuq ?大声笑这个想法是让OP弄清楚。我很高兴你不能两次投票。请参阅我使用的数字序列。它与 OP 想要的不同 ;)
    • 希望我可以再次投票,OP 要求的代码中没有解释 cmets。反正你的内存优化不错。
    • @BatScream 收到这个问题时,OP 去面试了。我想我在代码中已经说得够多了:)
    【解决方案3】:

    是否有明确的递归要求?它是由未在任何地方提及的问题的结构所暗示的。

    int rows=2 可能是一个例子,为了解决这个问题,你不能做任何“聪明”的事情,比如使用指针......

    我还将假设您不允许使用值 '> 100',以便您可以重载 n 值的含义 - 2 的补码也是如此。

    如果您允许循环,作为递归的替代,您可以生成三角形而无需将状态保存在堆栈之外:

    public static void main(String[] args){
        int rows=3;
        mirrorTriangle(rows);
    }
    
    public static void mirrorTriangle(int n){
    
        for (int i = 0 ; i < n + 1 ; i++) {
    
            renderLine(i);
        }
    
        for (int i = n ; i > 0 ; i--) {
    
            renderLine(i);
        }
    }
    
    private static void renderLine(int n) {
    
        int j = n * (n - 1) / 2 + 1;
        int k = j + n;
    
        while (j < k) {
    
            System.out.print(j);
            j++;
            if (j < k) System.out.print('*');
        }
    
        System.out.println();
    }
    

    【讨论】:

      【解决方案4】:

      试试这个新代码:

      public class String4 {    
          public static void main(String[] args) {    
              int rows = 3;    
              mirrorTriangle(rows);    
          }    
          private static void mirrorTriangle(int rows) {    
              for(int i=1;i<=rows;i++)    
              {    
                  for(int j=1;j<=i;j++)    
                  {    
                      System.out.print(i);    
                      if(j>0&&j<i)    
                      System.out.print("*");    
                  }    
                  System.out.println();    
              }    
              for(int k=rows;k>0;k--)    
              {    
                  for(int l=1;l<=k;l++)    
                  {    
                      System.out.print(k);    
                      if(l>0&&l<k)    
                          System.out.print("*");    
                  }    
                  System.out.println();    
              }    
          }    
      }
      

      输出:

      1 2*2 3*3*3 3*3*3 2*2 1

      【讨论】:

      • 代码在 StackOverflow 上缩进 4 个空格。您可以选择代码并按Ctrl+K同时缩进所有代码。
      【解决方案5】:

      我认为这是一个比选择的更好和简单的解决方案。

      public static void main(String[] args) {
          Scanner s = new Scanner(System.in);
          System.out.println("Enter limit");
          int limit = s.nextInt();
          int start[] = new int[limit];
          int v = 1;
          for (int i=1; i<=limit; i++) {
              start[i-1] = v;
                  for (int j=1; j<=i; j++) {
                      System.out.print(v++);
                      if(j==i)
                          continue;
                      System.out.print("*");
                  }
                  System.out.print("\n");
          }
          for (int i=limit-1; i>=0; i--) {
              v=start[i];
                  for (int j=i; j>=0; j--) {
                      System.out.print(v++);
                      if(j==0)
                          continue;
                      System.out.print("*");
                  }
                  System.out.print("\n"); 
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-13
        • 1970-01-01
        • 1970-01-01
        • 2014-09-08
        相关资源
        最近更新 更多