当前位置:网站首页>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

边栏推荐
- C. Unstable String
- 4. functions and inline functions with default values for formal parameters
- Random decoding NLP
- Vector 2 (friend and copy construction)
- "Initial C language" (Part 2)
- Summary of the first week of winter vacation
- [hdu] P7079 Pty loves lines
- 7.new, delete, OOP, this pointer
- 3D打印微组织
- Development status of full color LED display
猜你喜欢

Branch and loop statements (including goto statements) -part1

Constexpr keyword

Lexical Sign Sequence

Day367: valid complete square

3D printing microstructure

Using WordPress to create a MySQL based education website (learning notes 2) (technical notes 1) xampp error1045 solution

Network module packaging

SQL programming task05 job -sql advanced processing

Local deployment and problem solving of IIS in ArcGIS JS 4.23

office2016+visio2016
随机推荐
MySQL basic command statement
Phantomjs Usage Summary
[luogu] p1083 [noip2012 improvement group] classroom borrowing (difference)
SQL programming task05 job -sql advanced processing
Knowledge point learning
4. functions and inline functions with default values for formal parameters
Do you know the memory components of MySQL InnoDB?
Extend your kubernetes API using the aggregation API
Vector 2 (friend and copy construction)
Local deployment and problem solving of IIS in ArcGIS JS 4.23
9. class and object practice and initialization list
Express framework installation and start service
总体标准差和样本标准差
Use elk to save syslog, NetFlow logs and audit network interface traffic
It's still like this
Detailed explanation of clip attribute parameters
C#.NET万能数据库访问封装类(ACCESS、SQLServer、Oracle)
"Initial C language" (Part 1)
Cmake passing related macros to source code
SYSTEMd summary