C++ 打印带星号的图案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19734399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing out patterns with asterisks
提问by Javad Arjmandi
So this teacher assigned us to write 2 code that one of them prints out a triangle-like pattern, with character '*'!And he told us to do so, using the loop for.I managed to do it with the following code:
所以这位老师让我们写 2 个代码,其中一个打印出一个三角形的图案,带有字符“*”!他告诉我们这样做,使用循环 for。我设法用以下代码做到了:
#include <iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<11;i++)
{
cout<<endl;
for(j=1;i>j;j++)
{
cout<<'*';
}
}
for(i=10;i>0;i--)
{
cout<<endl;
for(j=1;i>j;j++)
{
cout<<'*';
}
}
cin.get();
return 0;
}
And it ran flawless. But my mind got frozen when I saw the second one! He's said to write a code that prints out a pattern like this:
它运行完美。但是看到第二个的时候我的心都凉了!据说他编写了一个代码来打印出这样的模式:
*
***
*****
*******
*****
***
*
I have no idea how to even start writing it! Can someone help?
我什至不知道如何开始写它!有人可以帮忙吗?
回答by ziolekjj
#include <iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<11;i++)
{
cout<<endl;
for (j=11; i<j; j--)
{
cout<< ' ';
}
for(j=1;i>j;j++)
{
cout<<'*';
}
for(j=1;i>j;j++)
{
cout<<'*';
}
}
for(i=10;i>0;i--)
{
cout<<endl;
for (j=11;i<j; j--)
{
cout<< ' ';
}
for(j=1;i>j;j++)
{
cout<<'*';
}
for(j=1;i>j;j++)
{
cout<<'*';
}
}
cin.get();
return 0;
}

