作者pziyout (pziyout)
看板Programming
標題Re: [問題] c語言聖誕樹並排
時間Wed Apr 29 11:56:31 2009
這只是一個普通的邏輯題目,只要注意迴圈下標之間的關係即可,
以下是用 C++ 寫的:
cout << "> tree number : " ;
cin >> n ;
for ( j = 1 ; j <= 3*n+1 ; ++j ) {
for ( i = 1 ; i <= 2*n-1 ; ++i ) {
k = ( i <= n ? i : 2*n-i ) ;
if ( j <= 3*(n-k) ) {
cout << string(4*k+1,' ') ;
} else if ( j <= 3*(n-k)+2*k+1 ) {
cout << string(3*n-k-j+1,' ')
<< string(2*(j-3*(n-k))-1,'*')
<< string(3*n-k-j+1,' ') ;
} else {
cout << string(2*k,' ') << '|' << string(2*k,' ') ;
}
cout << " " ;
}
cout << endl ;
}
cout << string(4*n*n+4*n-3,'=') << endl ;
輸出:
> tree number : 2
*
***
*****
* ******* *
*** ********* ***
***** | *****
| | |
=====================
> tree number : 3
*
***
*****
* ******* *
*** ********* ***
***** *********** *****
* ******* ************* ******* *
*** ********* | ********* ***
***** | | | *****
| | | | |
=============================================
> tree number : 4
*
***
*****
* ******* *
*** ********* ***
***** *********** *****
* ******* ************* ******* *
*** ********* *************** ********* ***
***** *********** ***************** *********** *****
* ******* ************* | ************* ******* *
*** ********* | | | ********* ***
***** | | | | | *****
| | | | | | |
=============================================================================
輸出還滿好看的。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.115.25.24
推 costbook:圖案有那麼一點點碎形的味道 203.68.92.156 04/29 12:09
推 bobju:這個比較漂亮 :) 59.104.191.26 04/29 14:21