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; } }

C++打印杨辉三角形

 

原文链接: https://www.cnblogs.com/tanghaiyong/p/11552581.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++打印杨辉三角形

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/302933

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 下午11:47
下一篇 2023年2月15日 下午11:48

相关推荐