杨辉

C++打印杨辉三角形

ぃ、小莉子 提交于 2019-11-30 03:33:32
#include <iostream> #include <iomanip> #include <Windows.h>using namespace std; #define N 256 void print_pyramid(int a[N][N], int lines); int main(void) { int n = 0; int a[N][N] = { 0 }; cout << "请输入要打印的杨辉三角形行数:"; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { if (j == 0 || j == i) { a[i][j] = 1; } else { a[i][j] = a[i - 1][j - 1] + a[i - 1][j]; } } } print_pyramid(a, n); system("pause"); return 0; } void print_pyramid(int a[N][N],int lines) { for (int i = 0; i < lines; i++) { int width = (lines - i) * 2; cout << setw(width) << a[i][0]; for (int j = 1; j <= i; j++) {