【C语言】每日一代码:杨辉三角

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 

#define Rows 100
#define Cols 100

int main()
{
	int arr1[Rows][Cols] = { 0 };
	int num = 0;
	scanf("%d", &num);
	assert(num < Rows);
	for (int i = 0; i < num; i++)
	{
		for (int j = 0; j <= i; j++)
		{
			if (j == 0 || j == i)
			{
				arr1[i][j] = 1;
			}
			else
			{
				arr1[i][j] = arr1[i - 1][j - 1] + arr1[i - 1][j];
			}
		}
	}
	for (int i = 0; i < num; i++)
	{
		int blank = num - i - 1;
		while (blank--)
		{
			printf(" ");
		}
		for (int j = 0; j <= i; j++)
		{
			printf("%2d ", arr1[i][j]);
		}
		printf("\n");
	}
	return 0;
}

你可能感兴趣的:(c语言,算法,开发语言)