c++杨辉三角

打印三到十行的杨辉三角

#include <iostream>
using namespace std;

void triangle(int n);

int main()
{
	int num;
	do{
		cout << "请输入行数: ";
		cin >> num;
	}while(num < 3 || num > 10);

	triangle(num);

	return 0;
}

void triangle(int n)
{
	int a[10][10];
	a[0][0] = 1;
	a[1][0] = 1;
	a[1][1] = 1;
	cout << a[0][0] << endl << endl << a[1][0] << "\t" << a[1][1] << endl << endl;

	for(int i = 2; 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];
			}

			cout << a[i][j] << "\t";
		}
		cout << endl << endl;
	}
}

    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/wuhui2356/article/details/48526075
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞