⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wex10_10.cpp

📁 数据结构C++代码,经典代码,受益多多,希望大家多多支持
💻 CPP
字号:
#include <iostream.h>
#include <iomanip.h>
#pragma hdrstop

// print Pascal's triangle for n
void PascalTriangle(int n)
{
	// use the two dimensional array C to hold the triangle
	int C[11][11];
	int i,j;
	
	// we only print the triangle for n <= 10
	if (n > 10)
	{
		cout << "n is limited to the range 1 to 10" << endl;
		return;
	}

	// build each row of the triangle in C, printing
	// each element as it is determined
	for(i=0;i < n+1;i++)
	{
		// build row i. the entries stop at the diagonal
		for(j=0;j <= i;j++)
		{
			// all the entries in column 0 are 1
			if (j == 0)
				C[i][j] = 1;
			// all entries on the diagonal are 1
			else if (j == i)
				C[i][j] = 1;
			else
			// all other entries are the sum of those in
			// the row above in the same column and the
			// column to the left
				C[i][j] = C[i-1][j-1] + C[i-1][j];
			// output the entry we just computed
			cout << setw(5) << C[i][j];
		}
		cout << endl;
	}				
}

void main(void)
{
	// print Pascal's triangle for n = 10
	PascalTriangle(10);
}

/*
<Run>

    1
    1    1
    1    2    1
    1    3    3    1
    1    4    6    4    1
    1    5   10   10    5    1
    1    6   15   20   15    6    1
    1    7   21   35   35   21    7    1
    1    8   28   56   70   56   28    8    1
    1    9   36   84  126  126   84   36    9    1
    1   10   45  120  210  252  210  120   45   10    1
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -