late night hand tear triangle

In the c language, for loops can be used to do a lot of things. What I want to share with you today is to print out two triangles that are close together through for loops. Why don't I say it is a rhombus? Hey, I'm asking questions, and there is the famous Yang Hui triangle problem

insert image description here

Yang Hui Triangle

Yanghui's triangle is essentially a triangle formed by the coefficients of each item after the nth power expansion of the binomial (a+b). Its properties include: the endpoint number of each row is 1, and a number is also 1; each number is equal to the sum of the two numbers on its upper left and upper sides.

How to use C language to realize Yang Hui's triangle? ? ?
insert image description here
figure 1

This is the final effect to be achieved.
If I remove the spaces in the printing, it will be the following effect
insert image description here
Figure 3

The number of digits may be different. It is not clear. What if every number is the same? ?
insert image description here
Figure 4

It can be seen from here that we can put the data in a two-dimensional array. For example,
int main()
{ int arr[10][10] = { 0 }; int i = 0;

for (i = 0; i <10; i++)
{
	int j=0;
	
	
	for (j = 0; j <=i; j++)    //这里j<=i就是为了打印主对角线下方的数据
	{
		printf("%d ", arr[i][j]);
	}
	printf("\n");
}

}
According to Figure 3, we can see that the first column of the two-dimensional array is all 1, and the main diagonal is also 1. The code is implemented as follows

int main()
{
int arr[10][10] = { 0 };
int i = 0;

for (i = 0; i < 10; i++)
{
	int j = 0;
	for (j = 0; j < 10; j++)
	{
		if(j == 0)//如果是第一列赋值为1
		{
			arr[i][j] = 1;
		}
		if (i==j)//如果是主对角线赋值为1
		{
			arr[i][j] = 1;
		}
	
    }

}
for (i = 0; i <10; i++)
{
int j=0;

	for (j = 0; j <=i; j++)
	{
		printf("%d ", arr[i][j]);
	}
	printf("\n");
}

}
The print results are as follows
insert image description here
, but there are still differences between this picture and Figure 3. By comparison, it is found that to realize the data of Yang Hui's triangle,

insert image description here

The data in the red circle below should be the sum of the two data in the red circle above, that is, arr[2][1]=arr[1][1]+arr[1][0];

arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];`
int main()
{
    
    
	int arr[10][10] = {
    
     0 };
	int i = 0;

	for (i = 0; i < 10; i++)
	{
    
    
		int j = 0;
		for (j = 0; j < 10; j++)
		{
    
    
			if(j == 0)
			{
    
    
				arr[i][j] = 1;
			}
			if (i==j)
			{
    
    
				arr[i][j] = 1;
			}
			if (i >= 1 && j >= 1)
			{
    
    
				arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
		
        }
}
	for (i = 0; i <10; i++)
	{
    
    
		int j=0;
		
		
		for (j = 0; j <=i; j++)
		{
    
    
			printf("%d ", arr[i][j]);
		}
		printf("\n");
	}
}

Now we get the result of Figure 3. In order to appear in the shape of a triangle, start to add spaces,
add spaces before printing each line, and the number of spaces in each line will decrease. We can achieve this by looping

int main()
{
    
    
	int arr[10][10] = {
    
     0 };
	int i = 0;

	for (i = 0; i < 10; i++)
	{
    
    
		int j = 0;
		for (j = 0; j <=i; j++)
		{
    
    
			if (j == 0)
			{
    
    
				arr[i][j] = 1;
			}
			if (i == j)
			{
    
    
				arr[i][j] = 1;
			}
			if (i >= 1 && j >= 1)
			{
    
    
				arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];

			}
		}
	}
		for (i = 0; i < 10; i++)
		{
    
    
			int j = 0;
			for (int k = 0; k < 10 - i; k++)//循环打印空格,空格数随着行数递减,i越大循环次数越小,打印空格越少,最后保证在最后一行i=9时,前面有一个空格,这里的循环判断可以写k < 10 - i
		{
    
    
			printf("  ");//两个空格
		}

			for (j = 0; j <=i; j++)
			{
    
    
				printf("%3d", arr[i][j]);
			}
			printf("\n");
		}
	}
	printf("  ");//两个空格
```c
在这里插入代码片
printf("%3d ", arr[i][j]);

It will look better if you print it like this.

# 打印菱形

![在这里插入图片描述](https://img-blog.csdnimg.cn/41cfb3f89f6c4c389ffe4684d3c659fa.png#pic_center)

这个菱形的打印我们可以分为上半部分和下半部分,可以从键盘输入上面的三角为n行,而下面的三角则为n-1行,我们先实现上半部分

```c
int n = 0;

	scanf_s("%d", &n);//通过键盘输入n行

	for (int i = 1; i <= n; i++)//循环遍历每一行

	{

		int j = 0;

		for (j = 0; j < n - i; j++)//为了使上半部分最后一行刚好与左边界对齐,循环判断条件为j < n - i当i==n时刚好打印空格的循环不进行循环,从第一行往下每一行少打印一个空格

		{

			printf(" ");



		}

		for (int k = 0; k < 2 * i - 1; k++)//打印*,观察规律打印*次数与行数有关,i为行数,*的数目为2*i-1

		{

			printf("*");

		}

		printf("\n");每一行结束打印换行


	}

insert image description here

Input n=7, seven lines in the first half, and
the following n-1 lines use a similar method
, `for (int i = 1; i <= n - 1; i++)//cycle through the next n-1 lines

{

	int j = 0;

	for (j = 0; j < i; j++)//为了和上面对称,并且空格的数目递增,1, 2 ,3这样增加,可以把循环条件与i联系起来,i每次jia1,j就可以多循环1次,多打印一个空格,循环判断条件可以写成j < i

	{

		printf(" ");



	}

	for (int q = 0; q < 2 * (n - 1 - i) + 1; q++)//根据下面打印的结果,下面第一行打印的应该是11个*,n=7,n-1=6,打印的次数为2*(n-1)-1吗??因为打印的*的次数每一行时变化的,所以应该和i这个变化的行数结合起来,下面第一行i=1;这一行打印了2*(n-1-i)+1次,

	{

		printf("*");

	}

	printf("\n");

}

insert image description here
overall code

int main()

{
    
    

	int n = 0;

	scanf_s("%d", &n);

	for (int i = 1; i <= n; i++)

	{
    
    

		int j = 0;

		for (j = 0; j < n - i; j++)

		{
    
    

			printf(" ");



		}

		for (int k = 0; k < 2 * i - 1; k++)

		{
    
    

			printf("*");

		}

		printf("\n");

	}


	for (int i = 1; i <= n - 1; i++)

	{
    
    

		int j = 0;

		for (j = 0; j < i; j++)

		{
    
    

			printf(" ");



		}

		for (int q = 0; q < 2 * (n - 1 - i) + 1; q++)

		{
    
    

			printf("*");

		}

		printf("\n");

	}}

You can print a lot of good-looking patterns through the for loop, except for the triangle pattern above, I hope you will support me a lot, if there is something wrong, please give me a lot of advice, thank you all

Guess you like

Origin blog.csdn.net/yyqzjw/article/details/131999986