当前位置:网站首页>[C language practice - printing hollow upper triangle and its deformation]

[C language practice - printing hollow upper triangle and its deformation]

2022-06-26 15:27:00 Beginners of C language

Active address : Graduation season · The technique of attack er

It's hot in summer , We welcome the graduation season in the heat wave , This is farewell , It is also the beginning of a new starting point . This is the month of graduation , Every year I see many graduates leave school to work , Students prepare for exams and summer internships , Very emotional , No matter what , As a student in school , While playing well , Still want to be able to learn in a down-to-earth way , Lay a solid foundation , From the beginning C Beginning of language , Prepare for the follow-up internship .


Preface

The front is already in 【C Language practice —— Print the upper triangle and its deformation 】【C Language practice —— Print the upper triangle and its deformation ( Blank version )】【C Language practice —— Print the hollow lower triangle and its deformation 】 Practiced printing upper triangle and its deformation 、 Print the hollow triangle and its deformation .

On this basis , Practice printing hollow upper triangles and their deformations , As shown in the figure below : They are the blank free versions of the upper triangle 、 Blank version 、 Hollow version

 Insert picture description here


1、 Print white top triangle

1.1 Hollow upper triangle left aligned version

// Print white top triangle ——— Left aligned version  
int main()
{
    
	int n = 0;
	while (scanf("%d", &n) != EOF)
	{
    
		for (int i = 0; i < n; i++)
		{
    
			int j = 0;
			for (j = 0; j < n - i; j++)
			{
    // Output when conditions are met  * , That is, on the boundary is  *
				if (i == 0 || j == 0 || j == n - i - 1 )
					printf("* ");		
				else
					printf(" ");// All points not on the boundary are spaces  
			}
			printf("\n");
		}
	}
	return 0;
}

The results are as follows :

 Insert picture description here

1.2 Hollow upper triangle middle aligned version

// Print white top triangle ——— Middle aligned version  
int main()
{
    
	int n = 0;
	while (scanf("%d", &n) != EOF)
	{
    
		for (int i = 0; i < n; i++)
		{
    
			int j = 0;
			for (j = 0; j <= i; j++)
			{
    
				printf(" ");
			}
			for (j = 0; j < n - i; j++)
			{
    // Output when conditions are met  * , That is, on the boundary is  *
				if (i == 0 || j == 0 || j == n - i - 1 )
					printf("* ");		
				else
					printf(" ");// All points not on the boundary are spaces  
			}
			printf("\n");
		}
	}
	return 0;
}

The results are as follows :

 Insert picture description here

1.3 White upper triangle right aligned version

// Print white top triangle ——— Right aligned version 
int main()
{
    
	int n = 0;
	while (scanf("%d", &n) != EOF)
	{
    
		for (int i = 0; i < n; i++)
		{
    
			int j = 0;
			for (j = 0; j <= 2 * i; j++)
			{
    
				printf(" ");
			}
			for (j = 0; j < n - i; j++)
			{
    // Output when conditions are met  * , That is, on the boundary is  *
				if (i == 0 || j == 0 || j == n - i - 1)
					printf("* ");
				else
					printf(" ");// All points not on the boundary are spaces  
			}
			printf("\n");
		}
	}
	return 0;
}

The results are as follows :

 Insert picture description here


summary

In this paper, we practice printing the deformation of the upper triangle of the hollow , Mainly practice :

  • Outer loop 、 Application of internal circulation
  • Pay attention to the number of lines 、 Space number 、 Symbol * Mathematical expressions between numbers

Active address : Graduation season · The technique of attack er

原网站

版权声明
本文为[Beginners of C language]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261503486827.html