2013-11-01 76 views
0

所以这位老师让我们写2个代码,其中一个打印出一个三角形样式,并带有'*'字符!他告诉我们这样做,使用循环。使用下面的代码:用星号打印图案

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

它运行完美无瑕。但是当我看到第二个时,我的脑海就冻结了!据说他写了一个打印出如下图案的代码:

* 
    *** 
***** 
******* 
***** 
    *** 
    * 

我不知道如何开始写它!有人可以帮忙吗?

+2

你有一个程序,打印你想要的右边一半。你会如何修改它以打印左半部分?现在,你会如何将它们粘在一起? – zwol

+0

http://stackoverflow.com/questions/10456654/print-star-diamond-in-c-with-nested-loops http://stackoverflow.com/questions/2094366/c-printing-ascii-heart-and-钻石与平台无关http://stackoverflow.com/questions/8598345/print-a-diamond-shape-with-a-borderusing-asterisks-in-cc http://stackoverflow.com/questions/1541487/ print-an-ascii-art-diamond等等......还有@Kunal,请好。 – indiv

+0

我总是这样解决这样的问题:'for(auto * p =“* \ n *** \ n ***** \ n ******* \ n ***** \ n *** \ n * \ n“; * p; std :: cout << * p ++);' – ecatmur

回答

-1
#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; 
}