加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C++打印杨辉三角形

发布时间:2020-12-16 09:14:50 所属栏目:百科 来源:网络整理
导读:#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 (
#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; } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读