当前位置:网站首页>Freshman C language summary post (hold change) Part1 output diamond

Freshman C language summary post (hold change) Part1 output diamond

2022-06-23 01:43:00 Mr_ Always

This series is a freshman C Language Xiaobai's last semester programming assignment , At the end of a semester, I'd like to make a simple arrangement . In the content part, there is the article of the great God , Some code snippets are also a bit troublesome , For the purpose of sharing and summarizing , You are welcome to correct the shortcomings !!!

【 Problem description 】 Give a number n ,2 <= n <= 9, Output diamond
【 Input form 】 A number
【 Output form 】 A diamond
【 The sample input 】4
【 Sample output 】

1

2 2

3 3 3

4 4 4 4

3 3 3

2 2

1

first line :3 After spaces , Output 1, And then go back ; Note that there are no extra spaces at the end

The second line :2 After spaces , Output 2, One more space , Then the output 2, And then go back ;

The third line :1 After spaces , Output 3 individual 3, There is a space between the two , the last one 3 Back is enter ...

......

The last line :3 After spaces , Output 1, And then go back ;

Ideas

Basic diamond output , It is easy to notice that the absolute value is used to keep the upper and lower symmetry , Control the number of lines and spaces

Code part reference :

#include<stdio.h>
#include<math.h>
int main()
{
	int n, j, k, l, m;
	scanf("%d", &n);
	n = n - 1;
	for ( j = -n; j <= n; j++)             // The number of control lines is 2n-1 
	{
		for (k = 1; k <= abs(j); k++)    // Output spaces per line  
			printf(" ");
			
		for (l = n - abs(j) + 1; l >= 2; l--)    
			printf("%d ", n - abs(j) + 1);       // Output number  ( With spaces )
			 
		printf("%d", n - abs(j) + 1);        // Output the last number of each line  
		printf("\n");            // Output carriage return  
	}
	return 0;
 } 

Real output case

原网站

版权声明
本文为[Mr_ Always]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220513599542.html