我是初学者,我必须打印这封信" N"超出了#。 到目前为止我只能打印| \,所以我仍然错过了最后一条腿#39; 我实际上并不知道我到目前为止如何......如果有人可以帮助我或解释! 这是我的代码:
#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 :(得分: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
- 循环):
for (i = 1; i <= 9; i++)
{
string s = "#";
s.append(i-1, ' ' );
s +='#';
s.append(9-i, ' ' );
s +='#';
cout << s << endl;
}
答案 1 :(得分:1)
我会选择&#34; Cheeting&#34;打印精确的东西而不用循环修复。
cout << "## #" << endl
cout << "# # #" << endl
cout << "# # #" << endl
cout << "# # #" << endl
cout << "# # #" << endl
cout << "# # #" << endl
cout << "# ##" << endl
很容易就像馅饼一样。
答案 2 :(得分:-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";};