Pascal's triangle 2d array - formatting printed output

前端 未结 2 1937
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-10 20:03

I have a small assignment where I have to use a 2d array to produce Pascal\'s triangle. Here is my code, and it works. there is an extra credit opportunity if I display the

2条回答
  •  不思量自难忘°
    2020-12-10 20:11

    Here I had modified your code, it prints wonderfully for ROW size till 13, coz of the limitation of my console window.

    import java.util.*; 
    
    public class Pascal { 
        public static final int ROW = 12;
        private static int max = 0;
    
        public static void main(String[] args) { 
            int[][] pascal  = new int[ROW +1][];
            pascal[1] = new int[1 + 2];
            pascal[1][1] = 1;
            for (int i = 2; i <= ROW; i++) {
                pascal[i] = new int[i + 2];
                for (int j = 1; j < pascal[i].length - 1; j++) {
                    pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];
                    String str = Integer.toString(pascal[i][j]);
                    int len = str.length();
                    if (len > max)
                        max = len;
                }
            }
    
    
            for (int i = 1; i <= ROW; i++) {                
                for (int k = ROW; k > i; k--)
                    System.out.format("%-" + max + "s", " ");
                for (int j = 1; j < pascal[i].length - 1; j++)                      
                    System.out.format("%-" + (max + max) + "s",  pascal[i][j]);
                System.out.println();
            }
        }
    }
    

    Hopefully this might help.

    Regards

提交回复
热议问题