打印杨辉三角C语言

#include<stdio.h>
void exchange(int arr1[], int arr2[], int n)
{
	int i = 0;
	for (i = 0; i < n; i++)
	{
		arr1[i] = arr2[i];
	}
}
void print(int arr1[10], int n)
{
	int i = 0;
	for (i = 0; i < n; i++)
	{
		printf("%d ", arr1[i]);
	}
	printf("\n");
}
int main()
{
	int i = 0;
	int n = 0;
	int arr1[11] = { 0 };
	int arr2[11] = { 0 };
	for (n = 1; n < 11; n++)
	{
		if (n < 3)
		{
			for (i = 0; i < n; i++)
			{
				arr1[i] = 1;
			}
			print(arr1, n);
		}
		else
		{
			for (i = 0; i < n; i++)
			{
				if (i == 0 || i == (n - 1))
				{
					arr2[i] = 1;
				}
				else
				{
					arr2[i] = arr1[i - 1] + arr1[i];
				}
			}
			exchange(arr1, arr2, n);
			print(arr1, n);
		}
	}
	return 0;
}

 

上一篇:算法


下一篇:javascript高级部分02补充----深拷贝和浅拷贝