码迷,mamicode.com
首页 > 其他好文 > 详细

杨辉三角

时间:2019-07-31 13:10:25      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:mes   iostream   end   cout   while   ++   mamicode   ima   using   

技术图片

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
	int n;
	int a[33][33];
	
	while(scanf("%d", &n) != EOF)
	{
		for(int i = 1; i <= n; ++ i)
		{
			a[i][1] = 1;
		}
		for(int i = 1; i <= n; ++ i)
		{
			for(int j = 1; j <= i; ++ j)
			{
				if(j == i)	a[i][j] = 1;
			}
		}
		
		for(int i = 1; i <= n; ++ i)
		{
			for(int j = 2; j < i; ++ j)
			{
				a[i][j] = a[i-1][j-1] + a[i-1][j];
			}
		}
		
		for(int i = 1; i <= n; ++ i)
		{
			for(int j = 1; j <= i; ++ j)
			{
				if(j == i)	cout << a[i][j];
				else	cout << a[i][j] << " ";
			}
			cout << endl;
		}
		cout << endl;
	}
	
	
	return 0;
}

  

杨辉三角

标签:mes   iostream   end   cout   while   ++   mamicode   ima   using   

原文地址:https://www.cnblogs.com/mjn1/p/11275351.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!