(显示金字塔)编写程序,提示用户输入1到15之间的整数并显示金字塔

时间:2019-05-05 20:51:32

标签: c++

我想创建一个创建金字塔的程序。其大小(行数)由用户输入确定。

这是我到目前为止尝试过的。它只显示了金字塔的一半。

#include <iostream>

using namespace std;

int main()
{
    //delcare all the variables
    int n, m, i, j;

    cout << "Enter the number of lines: ";
    cin >> n;

    for (i = 1; i <= n; i++)
    {
        //prints the spaces in between row and column using loop
        for(j = 1; j <= n - i; j++)
        {
            cout <<" ";
        }

        //prints the element of using loop
        for (j = i; j >= 1; j--)
        {
            cout <<" ";
        }

        //prints first and last elements using loop
        for (j = 2; j <= i; j++)
        {
            cout << " " << j << " ";
        }

        //elements in new line printed
        cout << "\n";
    }

    return 0;
}

当我输入数字7时,我期望整个金字塔,但是当前输出仅为金字塔的一半,即从2到7。

1 个答案:

答案 0 :(得分:2)

将变量声明为尽可能靠近变量的使用位置。给他们起有意义的名字:

#include <iostream>
#include <iomanip>

int main()
{
    std::cout << "Enter the number of lines: ";
    int num_lines;
    if (!(std::cin >> num_lines) || num_lines < 1 || 15 < num_lines) {
        std::cerr << "Input error. Bye :(\n\n";
        return EXIT_FAILURE;
    }

    for (int current_line{ 1 }; current_line <= num_lines; ++current_line) {
        for(int spaces = 0; spaces < num_lines - current_line; ++spaces)
            std::cout << "   ";

        for(int number = current_line; number; --number)
            std::cout << std::setw(2) << std::right << number << ' ';

        for (int number = 2; number <= current_line; ++number)
            std::cout << std::setw(2) << std::right << number << ' ';

        std::cout.put('\n');
    }
}

样本输出:

Enter the number of lines: 15

                                           1
                                        2  1  2
                                     3  2  1  2  3
                                  4  3  2  1  2  3  4
                               5  4  3  2  1  2  3  4  5
                            6  5  4  3  2  1  2  3  4  5  6
                         7  6  5  4  3  2  1  2  3  4  5  6  7
                      8  7  6  5  4  3  2  1  2  3  4  5  6  7  8
                   9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9
               10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10
            11 10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10 11
         12 11 10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10 11 12
      13 12 11 10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10 11 12 13
   14 13 12 11 10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10 11 12 13 14
15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

样本输入15的解释:

我正在使用std::wetw()将数字的输出填充到两位数。 std::right在这两个字符内右对齐输出:1 〜> " 1"11 〜> { {1}}。我这样做是为了在打印每行开头的空格时不必编写更复杂的代码。一个印刷的数字总是三个字符宽-两个代表数字,后面跟空格分隔数字。

谈论太空人物开始的每一行:在金字塔的底部,从15到1的所有数字,再到15再次被打印。因此,数字之前的空格数必须是第一行的三乘14倍,第二行的三乘以13倍,第三行的三乘以12倍,依此类推,直到最后一行(第15行)为三乘以零。如您所见,空格数是"11"乘以三。这就是第一个内部num_lines - current_line循环的作用。

另外两个内部for()循环应该是自我计划的。一个从for()倒数到current_line,另一个倒数到1。向上计数的循环从current_line开始,不会打印数字2的两倍。

相关问题