当前编程题:《打印W形图案》

【问题描述】

从键盘上输入n(1<n<21),按样例格式打印n行由字符 * 构成的W形图案。

【输入形式】

从键盘输入整数n (1<n<21)

【输出形式】

输出用字符 * 构成的W形图案,样式按下面的样例,第1行的起始字符位于第1列。

4

【样例输出】

*      *      *

 *    * *    *

  *  *   *  *

   *      *


下面附上我的代码:

#include <iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        for(int j= 0; j<i; j++)
        {
            cout << " ";
        }
        cout << "*";
        for(int k = 0; k < 2*(n-i)-3; k++)
        {
            cout << " ";
        }
        if(i==0)
        {
            cout << "*";
        }
        else if(i==(n-1))
        {


        }
        else
        {
            cout << "*";
            for(int m = 0; m < 2*i-1; m++)
            {
                cout << " ";
            }
            cout << "*";
        }
        for(int k = 0; k < 2*(n-i)-3; k++)
        {
            cout << " ";
        }
        if(i==(n-1))
        {
            for(int k = 0; k < 2*(n)-3; k++)
            {
                cout << " ";
            }
        }
        cout << "*";
        cout << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41682681/article/details/80772300