2016-11-14 69 views
-6

我是一名初學者,我必須從#中打印字母「N」。 到目前爲止,我只能打印| \,所以我仍然錯過了最後一個'腿'。 我真的不知道我是如何得到這個..如果任何人都可以幫助我解釋! 這裏是我的代碼:用#打印大寫字母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; 
} 
+0

您需要在每行的末尾打印一個'#'。如果你看看你的代碼,你應該找到你結束行的地方。你只需要改變它。 – NathanOliver

回答

4
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; 
} 

小更優雅(只使用一個for -loop):

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

我會去的 「Cheeting」 的方式打印確切的事情,而不重整與循環。

cout << "##  #" << endl 
cout << "# #  #" << endl 
cout << "# # #" << endl 
cout << "# # #" << endl 
cout << "# # #" << endl 
cout << "#  # #" << endl 
cout << "#  ##" << endl 

容易餡餅。

+0

你讓我的一天 – Treycos

-1
for(int y=0; y<9;y++){ 

    for(int i=0; i<9; i++){ 

    if((i==8&&y==0) or(i==8&&y==8)){std::cout<<" ";} 

    if(i==0 or i==8){std::cout<<"#";}else{std::cout<<" ";}; 

    if(i>0 && i<8){if(i==y){std::cout<<"#";std::cout<<" ";}else{std::cout<<" ";};}; 

    };std::cout<<"\n";}; 
+0

完美的對角線:D –