在C++建造两半金字塔

Build Two Half Pyramids in C++

本文关键字:金字塔 C++      更新时间:2023-10-16

我正在介绍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";
    }
}