const int nr = 7; //列的數量 const int nc = 2 * nr - 1; //行的數量 int[,] M = new int[nr, nc]; //建立一個 n x 2n-1大小的二維陣列 int row = 0, col = nc / 2; //計算第一列中間位置 M[row, col] = 1; //將1放入第1列中間位置 for (row = 1; row < nr; row++) { for (col = 0; col < nc; col++) { if (col == 0) M[row, col] = M[row - 1, col + 1]; //左邊界 else if (col == nc - 1) M[row, col] = M[row - 1, col - 1]; //右邊界 else M[row, col] = M[row - 1, col - 1] + M[row - 1, col + 1]; } } for (row = 0; row < nr; row++) { for (col = 0; col < nc; col++) { if (M[row, col] != 0) Console.Write("{0, 4}", M[row, col]); else Console.Write(" "); //如果是0的話,就只輸出4個空白,不要輸出0 } Console.WriteLine(); } }
Category: 程式設計