2017-05-03 4 views
-2

다음 패턴을 C++로 개발하고 싶습니다.높이와 너비가있는 C++ 스타 패턴

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


printf("Enter trunk height: "); 
scanf("%d", &trunk_height); 
printf("Enter trunk width: "); 
scanf("%d", &trunk_width); 
printf("Enter leaves width: "); 
scanf("%d", &leaves_width); 

TRUNC 높이 = 5
은 폭이 11

내가 코드를 작성하지만, 작동하지 않는 것입니다 잎 9
TRUNC 폭이다. 내 문제는 trunc의 높이를 감지하는 방법과 사용자로부터 정보를 얻는 코드를 작성하는 방법입니다.

int main() 
{ 
    int i, j; 
    int x = 5; 
    int y = 1; 

    for(j = 1; j <= 5; j++) 
    { 
     for(i = 1; i <= x; i++) 
     { 
      cout << " "; 
     } 
     x--; 

     for(i = 1; i <= y; i++) 
     { 
      cout << "*"; 
     } 
     y += 2; 

     cout << endl; 
    } 
} 
+1

그래서 질문은 무엇인가? –

답변

0

시도 다음 코드를

#include<iostream> 

using namespace std; 

int main() { 
    int height = 9, width = 5, leave = 11; 

    // Print tree 
    int stars = 1; 
    for(int row = (leave + 2 - 1)/2; row >= 1 ; row--){ 
     cout<<string(row, ' ')<<string(stars, '*')<<endl; 
     stars += 2; 
    } 

    // Print trunk 
    int pos = (leave - width)/2 +1; 
    for(int row = 0; row<height; row++){ 
     cout<<string(pos, ' ')<<string(width, '*')<<endl; 
    } 
} 

출력 :

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

Ideone link