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

[C language practice - printing square and its deformation]

2022-06-09 13:07:00 Beginners of C language


Preface

A square is a special parallelogram , See Baidu Encyclopedia for the definition

 Insert picture description here

This article is mainly about writing diamond exercises , Print diamonds on the screen .

 Insert picture description here


1、 Print graphics 1

// Print squares and their deformations 
int main()
{
    
	int n = 0;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)// Row number 
	{
    
		for (int j = 0; j < n; j++)// Number of columns 
		{
    
			printf("*");
		}
		printf("\n");
	}
	return 0;
}

The results are as follows , Although it is 10 That's ok 10 Column , Because there is no space between characters in the same line , And between different lines due to line breaks , The printed graphic does not look square , It's a rectangle :

 Insert picture description here

2、 Print graphics 2

// Print squares and their deformations 
int main()
{
    
	int n = 0;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)// Row number 
	{
    
		for (int j = 0; j < n; j++)// Number of columns 
		{
    
			printf("* ");
		}
		printf("\n");
	}
	return 0;
}

The results are as follows , It looks more like a square , But in fact, it is related to graphics 1 It is one thing .

 Insert picture description here

3、 Print graphics 3

Print a square , rotate 45 degree

// Print a square , rotate 45 degree , No blank space version 
int main()
{
    
	int n = 0;
	scanf("%d", &n);
	// Print the top half  n
	for (int i = 0; i < n; i++)
	{
    
		// Print a row 
		// Print space 
		int j = 0;
		for (j = 0; j < n - 1 - i; j++)
		{
    
			printf(" ");
		}
		// Print *
		for (j = 0; j < 2 * i + 1; j++)
		{
    
			printf("*");
		}
		printf("\n");
	}
	// Print the bottom half  n-1
	for (int i = 0; i < n - 1; i++)
	{
    
		// Print a row 
		// Print space 
		int j = 0;
		for (j = 0; j <= i; j++)
		{
    
			printf(" ");
		}
		// Print *
		for (j = 0; j < (n - 1 - i) * 2 - 1; j++)
		{
    
			printf("*");
		}
		printf("\n");
	}
	return 0;
}

** The results are as follows ,** It is still due to the inconsistent spacing of rows and columns , The square becomes a diamond :

 Insert picture description here

4、 Print graphics 4

// Print a square , rotate 45 degree , Blank version 
int main()
{
    
	int n = 0;
	scanf("%d", &n);
	// Print the top half  n
	for (int i = 0; i < n; i++)
	{
    
		// Print a row 
		// Print space 
		int j = 0;
		for (j = 0; j < n - 1 - i; j++)
		{
    
			printf(" ");
		}
		// Print *
		for (j = 0; j <= i; j++)
		{
    
			printf("* ");
		}
		printf("\n");
	}
	// Print the bottom half  n-1
	for (int i = 0; i < n - 1; i++)
	{
    
		// Print a row 
		// Print space 
		int j = 0;
		for (j = 0; j <= i; j++)
		{
    
			printf(" ");
		}
		// Print *
		for (j = 0; j <(n-1-i); j++)
		{
    
			printf("* ");
		}
		printf("\n");
	}
	return 0;
}

The results are as follows , The square is also a diamond , And graphics 3 The difference is whether there are spaces :

 Insert picture description here


summary

This article exercises printing squares and their deformations , Mainly practice :

  • Outer loop 、 Application of internal circulation
  • Pay attention to the number of lines 、 Space number 、 Symbol * The mathematical relationship between numbers

I didn't expect it before printing , The row and column intervals are different , The length and width of the square will be different . It can be seen that only the print results can verify whether the implementation results of your own code are consistent with your own ideas . The square became a combination of rectangle and diamond , I feel like I have written another article .

原网站

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