GZHU18级第二次周赛——白银等级 C - Problem C HDU - 2032

Time limit :1000 ms
Memory limit :32768 kB
OS :Windows
Source :
C语言程序设计练习(五)
问题描述 :

还记得中学时候学过的杨辉三角吗?具体的定义这里不再描述,你可以参考以下的图形:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Input
输入数据包含多个测试实例,每个测试实例的输入只包含一个正整数n(1<=n<=30),表示将要输出的杨辉三角的层数。
Output
对应于每一个输入,请输出相应层数的杨辉三角,每一层的整数之间用一个空格隔开,每一个杨辉三角后面加一个空行。
Sample Input
2 3
Sample Output
1
1 1

1
1 1
1 2 1

#include <iostream>
using namespace std;
int main()
{
	int n;
	int A[30][30] = { 0 };
	while (cin >> n)
	{
	
		int i = 0, j = 0;
		for (i = 0; i <= n - 1; i++)
		{
			for (j = 0; j <= i; j++)
			{
				if (j == 0 || i == j)
				{
					A[i][j] = 1;
				}
				else
				{
					A[i][j] = A[i - 1][j - 1] + A[i - 1][j];
				}
				if(i == j)
				cout << A[i][j] << endl;
			  else cout << A[i][j] << " ";
			}
		}
		cout <<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43981590/article/details/85057946