用 # 打印大写字母 N

Cou*_*out -5 c++ algorithm

我是初学者,我必须打印 # 中的字母“N”。到目前为止,我只能打印 |\ ,所以我仍然缺少最后一条“腿”。我实际上不知道我是如何做到这一点的..如果有人可以帮助我或解释一下!这是我的代码:

#include <iostream>
using namespace std;

int main()
{

    int i, j;

    for (i = 1; i <= 9; i++)
    {
        cout << "#";
        for (j = 1; j <= 12; j++)
        {
            if (i == j)
            {
                cout << "#";
            }
            else
            {
                cout << " ";
            }
        }

        cout << endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Sau*_*ahu 5

for (i = 1; i <= 9; i++)  //prints one line at a time
{
    cout << "#";
    for (j = 1; j <= 9; j++)
    {
        if (i == j) cout << "#";  //Diagonal part
        else cout << " ";
    }
    cout << "#";  // <<< You missed this
    cout << endl;
}
Run Code Online (Sandbox Code Playgroud)

更优雅一点(仅使用一个for循环):

for (i = 1; i <= 9; i++)
{
    string s = "#";
    s.append(i-1, ' ' );
    s +='#';
    s.append(9-i, ' ' );
    s +='#';
    cout << s << endl;
}
Run Code Online (Sandbox Code Playgroud)