首页 > 解决方案 > 对我的 C++ 金字塔作业进行小调整?我该怎么做?

问题描述

我的任务是为我的c++班级做家庭作业,但我无法弄清楚。

任务是:Create a program that will create a pattern in which is a pyramid. The user should enter the maximum number of rows to be output. Use a while loop that confirms the number of rows is between 1 and 9 inclusive. Next 1 should be output in the first row, 222 output in the second row, 33333 should be output in the third row, etc. For example if the user entered 7 the following would be output.

我现在的代码几乎完全做到了这一点,而不是输出,例如第二行的 222,它输出 2 2 这是我的代码的样子:

    #include <iostream>
using namespace std;
int main()
{
    int rows, count = 0, count1 = 0, k = 0;
    cout << "Please enter the number of rows." << endl;
    cin >> rows;
    while (rows > 9)
    {
        cout << "That is an invalid selection, please choose up to 9 rows." << endl;
        cin >> rows;
    }
    for (int i = 1; i <= rows; ++i)
    {
        for (int space = 1; space <= rows - i; ++space)
        {
            cout << " ";
            ++count;
        }
        while (k != 2 * i - 1)
        {
            if (count <= rows - 1)
            {
                cout << i << " ";
                ++count;
            }
            k++;
        }
        count1 = count = k = 0;
        cout << endl;
    }
}

任何帮助表示赞赏,我假设它应该只是一个小调整。

标签: c++loopsfor-loopwhile-loop

解决方案


这个循环

    while (k != 2 * i - 1)
    {
        if (count <= rows - 1)
        {
            cout << i << " ";
            ++count;
        }
        k++;
    }
    count1 = count = k = 0;

没有意义。例如,变量count1从不使用,除了语句

    count1 = count = k = 0;

所以不清楚定义这个变量的目的是什么。

使用标题中的操纵器,<iomanip>您可以只使用一个循环来编写金字塔。

这是一个演示程序

#include <iostream>
#include <iomanip>

int main() 
{
    while ( true )
    {
        const int MAX_HEIGHT = 9;

        std::cout << "Enter the height of a pyramid not greater than "
                  << MAX_HEIGHT << " (0 - exit): ";

        int height;

        if ( not ( std::cin >> height ) or ( height <= 0 ) ) break;

        if ( MAX_HEIGHT < height ) height = MAX_HEIGHT;

        std::cout << '\n';

        for ( int i = 0; i < height; i++ )
        {
            std::cout << std::setw( height - i ) << std::setfill( ' ' ) << i + 1;
            std::cout << std::setw( 2 * i + 1 ) << std::setfill( char( i + '1' ) ) << '\n';
        }

        std::cout << '\n';
    }

    return 0;
}

它的输出可能如下所示

Enter the height of a pyramid not greater than 9 (0 - exit): 1

1

Enter the height of a pyramid not greater than 9 (0 - exit): 2

 1
222

Enter the height of a pyramid not greater than 9 (0 - exit): 3

  1
 222
33333

Enter the height of a pyramid not greater than 9 (0 - exit): 4

   1
  222
 33333
4444444

Enter the height of a pyramid not greater than 9 (0 - exit): 5

    1
   222
  33333
 4444444
555555555

Enter the height of a pyramid not greater than 9 (0 - exit): 6

     1
    222
   33333
  4444444
 555555555
66666666666

Enter the height of a pyramid not greater than 9 (0 - exit): 7

      1
     222
    33333
   4444444
  555555555
 66666666666
7777777777777

Enter the height of a pyramid not greater than 9 (0 - exit): 8

       1
      222
     33333
    4444444
   555555555
  66666666666
 7777777777777
888888888888888

Enter the height of a pyramid not greater than 9 (0 - exit): 9

        1
       222
      33333
     4444444
    555555555
   66666666666
  7777777777777
 888888888888888
99999999999999999

Enter the height of a pyramid not greater than 9 (0 - exit): 0

推荐阅读