Pascal's Triangle Format

前端 未结 8 1889
长发绾君心
长发绾君心 2020-12-03 08:38

The assignment is to create Pascal\'s Triangle without using arrays. I have the method that produces the values for the triangle below. The method accepts an integer for the

8条回答
  •  广开言路
    2020-12-03 09:09

    This is a good start, where it's homework, I'll leave the rest to you:

    int maxRows = 6;
    int r, num;
    for (int i = 0; i <= maxRows; i++) {
        num = 1;
        r = i + 1;
        //pre-spacing
        for (int j = maxRows - i; j > 0; j--) {
            System.out.print(" ");
        }
        for (int col = 0; col <= i; col++) {
            if (col > 0) {
                num = num * (r - col) / col;
            }
            System.out.print(num + " ");
        }
        System.out.println();
    }
    

提交回复
热议问题