在C++建造两半金字塔
Build Two Half Pyramids in C++
我正在介绍C++课程,我的任务是用用户输入的高度构建两个半金字塔,中间有两个空格。它们应该看起来像这样。用户只能输入 1 到 23 之间的整数。
编辑:这是我老师想要的答案 - 她最初要求我们使用数组来制作这些,但后来改变了主意,改为使用for循环进行项目。
#include<iostream>
using namespace std;
int main(){
int i,j,h;
cout << "Welcome to Mario. Please enter an integer between 1 and 23." << endl;
cin >> h;
for(i=0;i<h;i++)
{
for(j=h;j>0;j--)
{
if (i+1 >= j)
cout << "#";
else
cout << " " ;
}
cout << " " ;
for (j=0;j<h;j++)
{
if (i>=j)
cout << "#";
}
cout << endl;
}
return 0;
}
生成的两个半金字塔
使用 2D 数组...虽然代码还不完整。相同的模式可以应用于 8-23。
// Example program
#include <iostream>
#include <string>
using namespace std;
int main()
{
int height ;
cout << "Please select height from 1 - 23" << endl;
cin >> height;
if (height == 1)
{
char array [1][13] =
{
{' ', ' ', ' ', ' ', '*', ' ',' ','*',' ',' ',' ',' ' }};
for (int x = 0; x < 1; x ++)
{
for (int y = 0; y < 13; y ++)
{
cout <<array[x][y] ;
}
cout << endl;
}
}
else if (height == 2)
{
char array [2][13] =
{
{' ', ' ', ' ', ' ', '*', ' ',' ','*',' ',' ',' ',' ' },
{ ' ', ' ', ' ','*','*', ' ',' ', '*','*', ' ',' ',' ',' '}};
for (int x = 0; x < 2; x ++)
{
for (int y = 0; y < 13; y ++)
{
cout <<array[x][y] ;
}
cout << endl;
}
}
else if (height == 3)
{
char array [3][13] =
{
{' ', ' ', ' ', ' ', '*', ' ',' ','*',' ',' ',' ',' ' },
{ ' ', ' ', ' ','*','*', ' ',' ', '*','*', ' ',' ',' ',' '},
{' ', ' ','*','*','*', ' ',' ', '*','*','*', ' ', ' ' }};
for (int x = 0; x < 3; x ++)
{
for (int y = 0; y < 13; y ++)
{
cout <<array[x][y] ;
}
cout << endl;
}
}
else if (height == 4)
{
char array [4][13] =
{
{' ', ' ', ' ', ' ', '*', ' ',' ','*',' ',' ',' ',' ' },
{ ' ', ' ', ' ','*','*', ' ',' ', '*','*', ' ',' ',' ',' '},
{' ', ' ','*','*','*', ' ',' ', '*','*','*', ' ', ' ' },
{' ', '*','*','*','*', ' ',' ', '*','*','*','*',' ',' ' }};
for (int x = 0; x < 4; x ++)
{
for (int y = 0; y < 13; y ++)
{
cout <<array[x][y] ;
}
cout << endl;
}
}
else if (height == 5)
{
char array [5][13] =
{
{' ', ' ', ' ', ' ', '*', ' ',' ','*',' ',' ',' ',' ' },
{ ' ', ' ', ' ','*','*', ' ',' ', '*','*', ' ',' ',' ',' '},
{' ', ' ','*','*','*', ' ',' ', '*','*','*', ' ', ' ' },
{' ', '*','*','*','*', ' ',' ', '*','*','*','*',' ',' ' },
{ '*','*','*','*','*',' ',' ', '*','*','*','*','*' }};
for (int x = 0; x < 5; x ++)
{
for (int y = 0; y < 13; y ++)
{
cout <<array[x][y] ;
}
cout << endl;
}
}
else if (height == 6)
{
char array [6][15] =
{
{' ',' ', ' ', ' ', ' ', '*', ' ',' ','*',' ',' ',' ',' ',' ' },
{ ' ',' ', ' ', ' ','*','*', ' ',' ', '*','*', ' ',' ',' ',' ',' '},
{' ',' ', ' ','*','*','*', ' ',' ', '*','*','*', ' ', ' ' ,' ' },
{' ',' ', '*','*','*','*', ' ',' ', '*','*','*','*',' ',' ',' ' },
{' ', '*','*','*','*','*',' ',' ', '*','*','*','*','*',' ' },
{'*', '*','*','*','*','*',' ',' ', '*','*','*','*','*','*' }};
for (int x = 0; x < 6; x ++)
{
for (int y = 0; y < 15; y ++)
{
cout <<array[x][y] ;
}
cout << endl;
}
}
else if (height == 7)
{
char array [7][17] =
{
{' ',' ',' ', ' ', ' ', ' ', '*', ' ',' ','*',' ',' ',' ',' ',' ',' ' },
{ ' ',' ',' ', ' ', ' ','*','*', ' ',' ', '*','*', ' ',' ',' ',' ',' ',' ' },
{' ',' ',' ', ' ','*','*','*', ' ',' ', '*','*','*', ' ', ' ' ,' ' ,' ' },
{' ',' ',' ', '*','*','*','*', ' ',' ', '*','*','*','*',' ',' ',' ',' ' },
{' ',' ', '*','*','*','*','*',' ',' ', '*','*','*','*','*',' ' ,' ' },
{' ','*', '*','*','*','*','*',' ',' ', '*','*','*','*','*','*' ,' ' },
{'*','*', '*','*','*','*','*',' ',' ', '*','*','*','*','*','*' ,'*' }};
for (int x = 0; x < 7; x ++)
{
for (int y = 0; y < 17; y ++)
{
cout <<array[x][y] ;
}
cout << endl;
}
}
else if (height == 8)
{
}
else if (height == 9)
{
}
else if (height == 10)
{
}
else if (height == 11)
{
}
else if (height == 12)
{
}
else if (height == 13)
{
}
else if (height == 14)
{
}
else if (height == 15)
{
}
else if (height == 16)
{
}
else if (height == 17)
{
}
else if (height == 18)
{
}
else if (height == 19)
{
}
else if (height == 20)
{
}
else if (height == 21)
{
}
else if (height == 22)
{
}
else if (height == 23)
{
}
}
你可以试试这个程序。它的工作原理是打印一个大三角形,然后用"空格"替换中间列。它在另一个 for 循环中使用一对 for 循环。
#include <iostream>
using namespace std;
int main() {
int n, i, sp, k;
cin >> n;
n += 1;
for (i = 1; i <= n; i++) {
for (sp = 1; sp <= n - i; sp++) {
cout<<" ";
}
for (k = 0; k != 2 * i - 1; k++) {
if (k == (2 * i - 1) / 2) {
cout << " ";
}
else cout << "#";
}
cout<<"n";
}
}
相关文章:
- C++星纹金字塔
- 我想通过OpenGL创建3D金字塔
- 现代OpenGL纹理金字塔
- 3D金字塔与OPENGL
- 对我的 c++ 金字塔分配进行小调整?我应该怎么做?
- 有没有办法用单行逻辑代码打印金字塔星形图案?面试官向我的朋友问了这个问题
- 如何打印出带有数字的完整金字塔
- (显示金字塔)编写一个程序,该程序提示用户输入1到15的整数并显示金字塔
- C++自上而下的金字塔
- 如何使程序谁将计算金字塔有多少个阶段
- 印刷半金字塔
- 画一个立方体和金字塔
- 在C++中制作金字塔 在程序中使用函数和循环
- 如何创建一个程序,用户输入整数并输出90度数字金字塔
- 在C 中未获得所需的1-0模式金字塔输出
- 图像调整大小与生成行人检测ROI的图像金字塔-OpenCV
- GPU加速LK金字塔中的窗口大小限制
- C++ 反数金字塔
- 在C++建造两半金字塔
- 高斯金字塔出界