C++打印杨辉三角形

#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++) { cout << setw(4) << a[i][j]; } cout << endl; } }

 

posted @ 2019-09-19 21:02  你爱过大海我爱过你  阅读(2524)  评论(0编辑  收藏  举报