function triangle(num) { var result = [[1], [1, 1]]; for (var i = 2; i < num; i++) { result.push([1]); for (var j = 1; j < i; j++) { result[i][j] = result[i - 1][j - 1] + result[i - 1][j]; } result[i][i] = 1; } return result; } var arr = triangle(5); console.log(arr[4].toString()); 上述代码运行后,输出结果是(1,4,6,4,1)。function triangle(num) { var result = [[1], [1, 1]]; for (var i = 2; i < num; i++) { result.push([1]); for (var j = 1; j < i; j++) {result[i][j] = result[i - 1][j - 1] + result[i - 1][j]; } result[i][i] = 1; } return result; } var arr = triangle(5);console.log(arr[4].toString()); 上述代码运行后,输出结果是(1,4,6,4,1)。
添加回答
举报
0/150
提交
取消