Search code examples
javafor-loopnested-loops

half pyramid inverted with matrix pattern


how to create a half pyramid inverted with matrix pattern like this with loop?

expected output

5 5 5 5 5 4 3 2 1
5 4 4 4 4 3 2 1
5 4 3 3 3 2 1
5 4 3 2 2 1
5 4 3 2 1

my code like this

public static void main(String[] args) {

        int N = 5;
        for (int i = 1; i <= N; i++)
        {
            for (int j = 1; j <= N; j++)
            {
                int min = i < j ? i : j;
                System.out.print(N - min + 1 + " ");
            }
            for (int k = N-1; k >=1; k --) {
                System.out.print(k + " ");
            }
            System.out.println();
        }

}

and my output like this

5 5 5 5 5 4 3 2 1
5 4 4 4 4 4 3 2 1
5 4 3 3 3 4 3 2 1
5 4 3 2 2 4 3 2 1
5 4 3 2 1 4 3 2 1

Solution

  • Your nested loops needs to be 3 deep:

    1. Loop over the rows:

      5 5 5 5 5 4 3 2 1  ┐
      5 4 4 4 4 3 2 1    ┤
      5 4 3 3 3 2 1      ┤  Loop 1
      5 4 3 2 2 1        ┤
      5 4 3 2 1          ┘
      
    2. Loop over the number values:

          ┌─────┬─┬─┬─┐     Loop 2
      ┌───┴───┐
      5 5 5 5 5 4 3 2 1
      5 4 4 4 4 3 2 1
      5 4 3 3 3 2 1
      5 4 3 2 2 1
      5 4 3 2 1
      └─┴─┴─┴─┘             Loop 2
      
    3. Loop over the repeats of a number value:

          ┌─────...
      ┌─┬─┼─┬─┐             Loop 3
      5 5 5 5 5 4 3 2 1
      5 4 4 4 4 3 2 1
      5 4 3 3 3 2 1
      5 4 3 2 2 1
      5 4 3 2 1
      

    Good luck coding that, since it looks like an assignment that you need to complete.


    Since I can't leave a simple challenge like that alone, here is a solution. I added logic so it can handle pyramid sizes above 9, and then compressed the code to obscure it a bit.

    static void printHalfPyramidInverted(int n) {
        String f, fmt = " %" + Integer.toString(n).length() + "d";
        for (int k, j, i = n; i > 0; i--, System.out.println())
            for (f = fmt.substring(1), j = n; j > 0; j--)
                for (k = (i == j ? j : 1); k > 0; k--, f = fmt)
                    System.out.printf(f, j);
    }
    

    Output (size 5)

    5 5 5 5 5 4 3 2 1
    5 4 4 4 4 3 2 1
    5 4 3 3 3 2 1
    5 4 3 2 2 1
    5 4 3 2 1
    

    Output (size 9)

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

    Output (size 15)

    15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1
    15 14 14 14 14 14 14 14 14 14 14 14 14 14 14 13 12 11 10  9  8  7  6  5  4  3  2  1
    15 14 13 13 13 13 13 13 13 13 13 13 13 13 13 12 11 10  9  8  7  6  5  4  3  2  1
    15 14 13 12 12 12 12 12 12 12 12 12 12 12 12 11 10  9  8  7  6  5  4  3  2  1
    15 14 13 12 11 11 11 11 11 11 11 11 11 11 11 10  9  8  7  6  5  4  3  2  1
    15 14 13 12 11 10 10 10 10 10 10 10 10 10 10  9  8  7  6  5  4  3  2  1
    15 14 13 12 11 10  9  9  9  9  9  9  9  9  9  8  7  6  5  4  3  2  1
    15 14 13 12 11 10  9  8  8  8  8  8  8  8  8  7  6  5  4  3  2  1
    15 14 13 12 11 10  9  8  7  7  7  7  7  7  7  6  5  4  3  2  1
    15 14 13 12 11 10  9  8  7  6  6  6  6  6  6  5  4  3  2  1
    15 14 13 12 11 10  9  8  7  6  5  5  5  5  5  4  3  2  1
    15 14 13 12 11 10  9  8  7  6  5  4  4  4  4  3  2  1
    15 14 13 12 11 10  9  8  7  6  5  4  3  3  3  2  1
    15 14 13 12 11 10  9  8  7  6  5  4  3  2  2  1
    15 14 13 12 11 10  9  8  7  6  5  4  3  2  1