Home >Backend Development >C++ >C++ program to print X star pattern
Star patterns showing different shapes, such as pyramids, squares and rhombuses, are a
Common parts of basic programming and logic development. we face various problems When we look at loop statements in programming, stars and number patterns are involved. This article demonstrates how to print an X or cross using an asterisk.We will see the same two methods. The first one is a bit complicated, but the next one The method is very efficient.
* * * * * * * * * * * * * * * * *
For this pattern, the number of rows is n = 5. This is for the top half. Total X patterns are 2n – 1
Let’s see how to do this using the following table −
Line number | The Chinese translation ofStar Count | is:number of stars | remaining space | The Chinese translation ofSpace Between | is:Spacing | describe | |
---|---|---|---|---|---|---|---|
1 | is:1 | 2 | 0 | 7 | When i = n, print a star, otherwise print 2. The space on the left is (i – 1), and the space between spaces is 2(n – i) - 1 | ||
2 | 2 | The translation of1 | is:1 | 5 | |||
3 | 2 | 2 | 3 | ||||
4 | is:4 | 2 | 3 | 1 | |||
5 | is:5 | The translation of1 | is:1 | The Chinese translation of4 | is:4 | The Chinese translation of- | is:- |
6 | 2 | 3 | 1 | The stars on the left are decreasing, such as n - (i - n) - 1 = 2n - i - 1. The number of spaces will follow: 2 * (i - n) - 1 | |||
7 | 2 | 2 | 3 | ||||
8 | 2 | The translation of1 | is:1 | 5 | |||
9 | is:9 | 2 | 0 | 7 |
#include <iostream> using namespace std; void solve( int n ){ for ( int i = 1; i <= 2*n - 1; i++ ) { if ( i <= n ) { for ( int j = 1; j <= i - 1; j++ ) { cout << ". "; } cout << "* "; if ( i != n ) { for ( int j = 1; j <= 2 * (n - i) - 1; j++ ) { cout << " "; } cout << "* "; } } else { for ( int j = 1; j <= (2 * n) - i - 1; j++ ) { cout << ". "; } cout << "* "; for ( int j = 1; j <= 2 * (i - n) - 1; j++ ) { cout << " "; } cout << "* "; } cout << "\n"; } } int main(){ int n = 8; cout << "X Star Pattern for " << n << " lines." << endl; solve( n ); }
X Star Pattern for 8 lines. * * . * * . . * * . . . * * . . . . * * . . . . . * * . . . . . . * * . . . . . . . * . . . . . . * * . . . . . * * . . . . * * . . . * * . . * * . * * * *
X Star Pattern for 10 lines. * * . * * . . * * . . . * * . . . . * * . . . . . * * . . . . . . * * . . . . . . . * * . . . . . . . . * * . . . . . . . . . * . . . . . . . . * * . . . . . . . * * . . . . . . * * . . . . . * * . . . . * * . . . * * . . * * . * * * *
The same problem can be solved by considering a grid, and from this grid, we can calculate The formula where the stars are printed and where the spaces are printed.
* | * | |||||||
* | * | |||||||
* | * | |||||||
* | * | |||||||
* | ||||||||
* | * | |||||||
* | * | |||||||
* | * | |||||||
* | * |
从上面的网格中,很容易理解,星星只会在列中放置 数字与行号(对角线)相同,列号为 (2n + 1 – i)
#include <iostream> using namespace std; void solve( int n ){ int m = 2*n - 1; for ( int i = 1; i <= m; i++ ) { for ( int j = 1; j <= m; j++ ) { if (j == i || j == (m + 1 - i)) cout << "* "; else cout << ". "; } cout << endl; } } int main(){ int n = 6; cout << "X Star Pattern for " << n << " lines." << endl; solve( n ); }
X Star Pattern for 6 lines. * . . . . . . . . . * . * . . . . . . . * . . . * . . . . . * . . . . . * . . . * . . . . . . . * . * . . . . . . . . . * . . . . . . . . . * . * . . . . . . . * . . . * . . . . . * . . . . . * . . . * . . . . . . . * . * . . . . . . . . . *
X Star Pattern for 8 lines. * . . . . . . . . . . . . . * . * . . . . . . . . . . . * . . . * . . . . . . . . . * . . . . . * . . . . . . . * . . . . . . . * . . . . . * . . . . . . . . . * . . . * . . . . . . . . . . . * . * . . . . . . . . . . . . . * . . . . . . . . . . . . . * . * . . . . . . . . . . . * . . . * . . . . . . . . . * . . . . . * . . . . . . . * . . . . . . . * . . . . . * . . . . . . . . . * . . . * . . . . . . . . . . . * . * . . . . . . . . . . . . . *
星形模式使用简单,对于学习编程循环思想很有用。这 文章演示了如何使用 C++ 显示左和右半菱形图案 右对齐的半菱形。拍摄后,X 或十字图案将使用星号显示 考虑n行计数。为此,我们提供了两种方法。一聘 填充和空白空间,而另一个则利用网格计算。而不是添加 空格,我们添加了点。如果没有,他们偶尔会从输出中删除空格。
The above is the detailed content of C++ program to print X star pattern. For more information, please follow other related articles on the PHP Chinese website!