在屏幕上打印 X 的形状

Print the shape of an X on screen

本文关键字:打印 屏幕      更新时间:2023-10-16

我想像这样在屏幕上打印一个X:

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

我尝试使用此代码:

int main(){
    bool back = false;
    for (int i = 0; i < 7; ++i) {
        if (i == 4)
            back = true;   
        if (!back){
            for (int j = 0; j < i; ++j) {
                cout << " ";
            }
        } else{
            for (int j = 7-i-1; j > 0; --j) {
                cout << " ";
            }
        }
        cout << "*" << endl;
    }
}

结果缺少右半部分:

*
 *
  *
   *
  *
 *
* 

问题是我无法弄清楚如何打印星星和跟随它们的星星之间的空间。

解决此问题的更具教育性的方法需要 2 个循环。

第一个for循环控制输出的高度,即打印的行数。每次迭代打印一行并以std::endl结束。

第二个是嵌套for循环,它控制宽度并水平打印字符,即它打印该行的星号和空格。每次迭代打印一个空格或一个星号。

此图可能有助于理解变量的值,当x_size = 5

                 (width)     
             0   1   2   3   4
(height)   ---------------------
   0       | * |   |   |   | * |      asterisk_pos = 0, end_pos = 4, inc =  1
           ---------------------
   1       |   | * |   | * |   |      asterisk_pos = 1, end_pos = 3, inc =  1
           ---------------------
   2       |   |   | * |   |   |      asterisk_pos = 2, end_pos = 2, inc =  1
           ---------------------
   3       |   | * |   | * |   |      asterisk_pos = 1, end_pos = 3, inc = -1
           ---------------------
   4       | * |   |   |   | * |      asterisk_pos = 0, end_pos = 4, inc = -1
           ---------------------

源代码

int main()
{
    int x_size = 7;        // size of the drawing
    int asterisk_pos = 0;  // initial position of the asterisk
    int inc = 1;           // amount of increment added to asterisk_pos after an entire line has been printed
    // height is the line number
    for (int height = 0; height < x_size; height++)
    {
        // width is the column position of the character that needs to be printed for a given line
        for (int width = 0; width < x_size; width++)
        {
            int end_pos = (x_size - width) - 1; // the position of the 2nd asterisk on the line
            if (asterisk_pos == width || asterisk_pos == end_pos)
                cout << "*";
            else
                cout << " ";
        }
        // print a new line character
        cout << std::endl;
        /* when the middle of x_size is reached, 
         * it's time to decrease the position of the asterisk!
         */
        asterisk_pos += inc;    
        if (asterisk_pos > (x_size/2)-1)
            inc *= -1;
    }    
    return 0;
}

x_size = 7输出:

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

x_size = 3输出:

* *
 * 
* *

观察每行中的序列。看看你拥有的第一部分:

    0 空格、1 *、5 空格、
  • 1 *、0 空格
  • 1 个空格、1 *、3 个空格、1
  • *、1 个空格
  • 2 个空格、1 *、1 个空格、
  • 1 *、2 个空格
然后对于行 i:i 空格后跟 1 *,后跟

5-2 i 空格,后跟 1 *,后跟 i 空格

那么以下内容应该有效:

for (int line=0; line<3; line++) {
    for (int n=0; n<line; n++) cout << ' ';
    cout << '*';
    for (int n=0; n<5-2*line; n++) cout << ' ';
    cout << '*';
    for (int n=0; n<line; n++) cout << ' ';
    cout << endl;
}

中间线3很明显,下面是第一部分的反面。

另一种方法是观察 * 的位置顺序:(0,6( (1,5( (2,4( (

3,3( (4,2( (5,1( (6,0(,因此:

for (int line=0; line<7; line++) {
    int pos1 = line;
    int pos2 = 6-line;
    for (int n=0; n<7; n++) {
        if (n==pos1 || n==pos2) cout << '*';
        else cout << ' ';
    }
    cout << endl;
}

然后,您可以明显地删除pos1pos2...

部之间的空格减少 2 并以 line - 2 开头

下部之间的空格被激怒了 2

这是我如何解决你的问题

void printSpaces(int count)
{
    for (int i = 0; i < count; ++i) {
        cout << " ";
    }
}
int main()
{
    int lines = 7;
    int spaceBefore = 0;
    int spaceBetween = lines - 2;
    bool backword = false;
    for (int i = 0; i < lines; ++i)
    {
        printSpaces(spaceBefore);
        cout << "*";
        if (spaceBetween > 0)
        {
            printSpaces(spaceBetween);
            cout << "*";
        }
        else
        {
            backword = true;
        }
        cout << "n";
        spaceBefore = backword ? spaceBefore-1 : spaceBefore+1;
        spaceBetween = backword ? spaceBetween+2 : spaceBetween-2;
    }
    return 0;
}

该模式由两个等式组成:x = yx + y = 4

只需循环遍历轴并绘制落在任何一条线上的点即可。

                 ( y )
           0   1   2   3   4
 ( x )   ---------------------
   0     | * |   |   |   | * |
         ---------------------
   1     |   | * |   | * |   |
         ---------------------
   2     |   |   | * |   |   |
         ---------------------
   3     |   | * |   | * |   |
         ---------------------
   4     | * |   |   |   | * |
         ---------------------
   Two Equations
   x = y
   x + y = 4

#include <iostream>    
int main() {
    int num_lines = 7;
    auto on_line1 = [](int x, int y) {
        return x == y;
    };
    auto on_line2 = [num_lines](int x, int y) {
        return (x + y) == (num_lines - 1);
    };
    for(int x = 0; x < num_lines; x++) {              // Simple looping
        for(int y = 0; y < num_lines; y++) {          // through the axes
            if(on_line1(x, y) or on_line2(x, y)) {    // If on any of the  line
                std::cout << '*';                     // Then plot it
            } else {
                std::cout << ' ';                     // Else leave it
            }
        }
        std::cout << 'n';
    }
    return 0;
}

PS:我从另一个答案中复制了ascii表。

如果您不需要循环,则可以创建一个string并打印它。

#include <iostream>
#include <string>
int main(int argc, char * argv[]){
    std::string myX("*     *n *   * n  * *  n   *   n  * *  n *   * n*     *n");
    std::cout << myX;
    return 0;
}