Re: [問題] c語言聖誕樹並排

看板Programming作者 (pziyout)時間16年前 (2009/04/29 03:56), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串2/3 (看更多)
這只是一個普通的邏輯題目,只要注意迴圈下標之間的關係即可, 以下是用 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

04/29 12:09, , 1F
圖案有那麼一點點碎形的味道
04/29 12:09, 1F

04/29 14:21, , 2F
這個比較漂亮 :)
04/29 14:21, 2F
文章代碼(AID): #19zy_moq (Programming)
文章代碼(AID): #19zy_moq (Programming)