Home >Backend Development >C++ >C++ program to print X star pattern

C++ program to print X star pattern

PHPz
PHPzforward
2023-09-13 11:45:031231browse

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.

Star pattern (using two sets of spaces)

*       *
 *     *
  *   *
   * *
    *
   * *
  *   *
 *     *
*       *

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 −

The Chinese translation of is: The Chinese translation of is: The translation of is: The translation of is: The Chinese translation of is: The Chinese translation of is: The translation of is: The Chinese translation of is: The Chinese translation of is: The translation of is: The translation of is:
Line numberStar Countnumber of stars remaining spaceSpace Between Spacing describe
11 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 211 5
3 2 2 3
44 2 3 1
551144--
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 211 5
99 2 0 7

algorithm

  • Read n as input
  • For the range of i from 1 to 2n - i, execute
    • If i
    • For j ranging from 1 to i - 1, execute
      • Show empty space
    • Finish
    • Show stars
    • If i and n are different, then
      • For j ranging from 1 to 2(n - i) - 1, execute
        • Show empty space
      • Finish
      • Show stars
    • If it ends
  • otherwise
    • For j ranging from 1 to 2n - i - 1, execute
      • Show empty space
    • Finish
    • Show stars
    • For j ranging from 1 to 2(i - n) - 1, execute
      • Show empty space
    • Finish
    • Show stars
  • If it ends
  • Move the cursor to the next line
  • Finish
  • Example

    #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 );
    }
    

    Output

    X Star Pattern for 8 lines.
    *                            *  
    . *                        *  
    . . *                    *  
    . . . *                *  
    . . . . *            *  
    . . . . . *        *  
    . . . . . . *    *  
    . . . . . . . *  
    . . . . . . *    *  
    . . . . . *        *  
    . . . . *            *  
    . . . *                *  
    . . *                    *  
    . *                        *  
    *                            *
    

    Output (n = 10)

    X Star Pattern for 10 lines.
    *                                    *  
    . *                                *  
    . . *                            *  
    . . . *                        *  
    . . . . *                    *  
    . . . . . *                *  
    . . . . . . *            *  
    . . . . . . . *        *  
    . . . . . . . . *    *  
    . . . . . . . . . *  
    . . . . . . . . *    *  
    . . . . . . . *        *  
    . . . . . . *            *  
    . . . . . *                *  
    . . . . *                    *  
    . . . *                        *  
    . . *                            *  
    . *                                *  
    *                                    *  
    

    Use Grid Method

    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)

    算法

    • 读取 n 作为输入
    • m = 2n - i
    • 对于从 1 到 m 的 i,执行
      • 对于 j 从 1 到 m 的范围,执行
        • 如果 j 与 i 相同或 j 与 (m + 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.
    * . . . . . . . . . * 
    . * . . . . . . . * . 
    . . * . . . . . * . . 
    . . . * . . . * . . . 
    . . . . * . * . . . . 
    . . . . . * . . . . . 
    . . . . * . * . . . . 
    . . . * . . . * . . . 
    . . * . . . . . * . . 
    . * . . . . . . . * . 
    * . . . . . . . . . * 
    

    输出(当n = 8时)

    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!

    Statement:
    This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete