杨辉三角

#include <stdio.h>
#include <stdlib.h>

#define N 10

void main()
{
	int a[N][N] = { 0 };

	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < i+1; j++)
		{
			if (j == 0 || i == j)
			{
				a[i][j] = 1;
			}
			else
			{
				a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
			}
		}
	}

	for (int i = 0; i < N; i++)
	{
		for (int k = 0; k < N - 1 - i; k++)
		{
			printf("  ");
		}
		for (int j = 0; j < i+1; j++)
		{
			printf("%4d", a[i][j]);
		}
		printf("\n");
	}
	system("pause");
}

 

posted @ 2016-01-05 12:03  喵小喵~  阅读(115)  评论(0编辑  收藏  举报