简体   繁体   English

三角形图案

[英]Triangle pattern

I am trying to get output like 我试图获得像

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

But my code is giving the wrong output. 但是我的代码给出了错误的输出。 My output is: 我的输出是:

Please enter the size: 9
*********
        ********
       *******
      ******
     *****
    ****
   ***
  **
 *

and my code is: 我的代码是:

#include<iostream>
using namespace std;

int main (void)
{
    int row, column, size;
    cout << "Please enter the size: ";
    cin >> size;
    cin.ignore(999,'\n');

    for (row = size; row <= size; row--)
    {
        if (row <= 0)
        {
            break;
        }
        else if (row == size)
        {
            for (column = 1; column <= size; column++)
            {
                cout << "*";
            }
        }
        else
        {
            for (column = row; column <= row; column--)
            {
                if (column <= 0)
                {
                    break;
                }
                cout << ' ';
            }
            for (column = row; column <= row; column--)
            {
                if (column <= 0)
                {
                    break;
                }
                cout << "*";
            }
        }
        cout << endl;
    }

    cout << endl;
    cout << "Please press enter to finish...";
    cin.ignore(999,'\n');
    return 0;
}

I don't know what's wrong and where it is but I am thinking the problem might be in the else loop. 我不知道问题出在哪里以及在哪里,但我认为问题可能出在else循环中。

Try to re-think your problem. 尝试重新考虑您的问题。 You have a lot of complex code to achieve something simple. 您有很多复杂的代码来实现简单的目标。 Your output should look like this: 您的输出应如下所示:

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

So some things to note about this: 因此,需要注意以下几点:

  1. The rows are all the same length! 行都一样长!
  2. The only difference is the number of spaces. 唯一的区别是空格数。 If we have row, 0 we have 0 spaces, row 4 has 4 spaces. 如果我们有行,则0有0个空格,第4行有4个空格。

So with this your code should be simple: 因此,您的代码应该很简单:

// PSEUDO CODE
for row = 0  to max_rows
    for i = 0 to max_rows
        if (i < row)
            print a space
        else 
            print a *

And that should do it. 那应该做到的。

What you are doing here is : 您在这里所做的是:

  1. print * s for first line (in amount of size) 第一行打印* s(以大小为单位)
  2. if not first line print size - loopCounter spaces then print size - loopCounter * s 如果不是第一行,则打印size - loopCounter空格,然后打印size - loopCounter * s

As you can see this algorithm can't get you the shape you want. 如您所见,该算法无法获得所需的形状。 Also why you loop backward and check for none negative value? 还有为什么要向后循环并检查是否没有负值? you don't need it. 您不需要它。 What you actually want is : 您真正想要的是:

  1. an outer loop to generate columns (prints new line) 一个外部循环以生成列(打印新行)
  2. an inner loop to generate data of each row 一个内部循环以生成每一行的数据

The only thing that important here is how to generate data of each row. 这里唯一重要的是如何生成每一行的数据。 as you can see the count of spaces in each row is equal to index of column (starting with 0). 如您所见,每行中的空格数等于列的索引(从0开始)。

Here's what you can try (I broke inner loop into two loops): 您可以尝试以下操作(我将内部循环分为两个循环):

#include<iostream>
using namespace std;

int main(void)
{
    int size;
    cout << "Please enter the size: ";
    cin >> size;
    cin.ignore(999, '\n');

    for(int column = 0; column < size; ++column)
    {
        for(int spaces = 0; spaces < column; ++spaces)
        {
            cout << " ";
        }
        for(int starts = 0; starts < size - column; ++starts)
        {
            cout << "*";
        }
        cout << endl;
    }

    cout << "Please press enter to finish...";
    cin.ignore(999, '\n');
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM