2013-11-01 78 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; 
}