Re: [問題] c語言聖誕樹並排
這只是一個普通的邏輯題目,只要注意迴圈下標之間的關係即可,
以下是用 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
討論串 (同標題文章)
完整討論串 (本文為第 2 之 3 篇):
問題
5
6