c++用for+if else打印金字塔

img


c++打印金字塔用如图语句for+if else

你题目的解答代码如下:

#include<iostream>
using namespace std;

int main()
{
    int x,y,nrows=6;
    for (y = 1; y <= nrows; y++)
    {
        for (x = 1; x <= nrows-y; x++)
            cout << "#";
        for (x = 1; x <= y*2-1; x++)
            if (x%2==1)
                cout << "*";
            else
                cout << "#";
        for (x = 1; x <= nrows-y; x++)
            cout << "#";
        cout << endl;
    }
    return 0;
}

如有帮助,望采纳!谢谢!

img