c++ - 对我的 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;
}
}
任何帮助表示赞赏,我假设它应该只是一个小调整。
解决方案
这个循环
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
推荐阅读
- cordova - Cordova 视频文件上传 Android 10 目标级别 29 不起作用
- android - 调用应用程序相机的 Matrix.org 元素在电视中不起作用
- python - groupby 根据 df2 python 的 bin 查找 df1 的最小值和最大值
- angular - 如何在ngrx中使用dispatch函数触发多个调用
- java - Weblogic 12c 无法解析类文件 hikaricp-3.4.5 module-info.class
- python - Python 气流 1.14。如何重新启动 dag 或 dag 中的一组任务
- c# - ASP.NET Core Identity - 在数据库中存储用户/密码/锁定设置
- apache - 如何自动管理网站的语言重定向但保持手动更改?
- delphi - 为什么在 android 64 库中添加 Jar 不起作用,仅在 android 32 位中
- mysql - MYSQL 查询总结某个字段的所有值