在屏幕上打印X的形状

时间:2018-01-26 16:06:17

标签: c++

我想在屏幕上打印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;
    }
}

结果是错过了右半边:

*
 *
  *
   *
  *
 *
* 

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

5 个答案:

答案 0 :(得分:3)

观察每一行的顺序。看看你的第一部分:

  • 0空格,1 *,5空格,1 *,0空格
  • 1个空格,1 *,3个空格,1 *,1个空格
  • 2个空格,1 *,1个空格,1 *,2个空格

然后对于行 i i 空格后跟1 *后跟5-2 i 空格,后跟1 *,然后是空格

然后以下内容应该有效:

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 ...

答案 1 :(得分:3)

解决此问题的更有教育意义的方法需要2个循环。

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

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

此插图可能有助于在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输出:

* *
 * 
* *

答案 2 :(得分:1)

上部之间的空格减少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;
}

答案 3 :(得分:1)

该模式由两个等式组成: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:我从other answer复制了ascii表。

答案 4 :(得分:0)

如果您不需要循环,则可以创建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;
}