文章

54

粉丝

66

获赞

54

访问

3.5k

头像
杨辉三角形 (模拟)题解:
P1062 北京大学/湖南大学机试题
发布于2024年4月25日 00:43
阅读数 45

#include <bits/stdc++.h>
using namespace std;

const int N = 30;
int n;
int res[N][N];

void get_triangle()
{
	for(int i = 1; i <= n; i ++)
	{
		for(int j = 1; j <= i; j ++)
		{
			if(j == 1 || j == i) res[i][j] = 1;
			else res[i][j] = res[i - 1][j] + res[i - 1][j - 1];
		}
	}
	
	for(int i = 1; i <= n; i ++)
	{
		for(int j = 1; j <= i; j ++)
			cout << res[i][j] << " ";
			
		cout << endl;
	}
}

int main()
{
	
	while(cin >> n, n)
	{
		memset(res, 0, sizeof res);
		get_triangle();
		cout << endl;
	}
	
    return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发