杨辉三角输入3输出11 11 2 1输入4输出11 11 2 11 3 3 1c++ code
#include
int main() { int n; cout << "请输入杨辉三角的行数:"; cin >> n; int a[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { if (j == 0 || j == i) { a[i][j] = 1; // 第一列和对角线上的元素都是1 } else { a[i][j] = a[i - 1][j - 1] + a[i - 1][j]; // 其他元素是上一行相邻两个元素的和 } cout << a[i][j] << " "; } cout << endl; } return 0; }
原文地址: https://www.cveoy.top/t/topic/c2KA 著作权归作者所有。请勿转载和采集!