ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์‚ผ๊ฐํ˜• ๊ทธ๋ฆฌ๊ธฐ
    ๊ฐœ๋ฐœ/Algorithm 2022. 3. 4. 09:50

    ๋ฌธ์ œ


    n์ธต์˜ ์‚ผ๊ฐํ˜•์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์—ฌ๋ผ. Input, Output์˜ ์˜ˆ์ œ๋ฅผ ์ฐธ๊ณ ํ•œ๋‹ค.

     

    ์ž…๋ ฅ


    ์ฒซ์งธ ์ค„์— ์ •์ˆ˜n์ด ์ฃผ์–ด์ง„๋‹ค. (0≤n≤100)

     

     

    ์ถœ๋ ฅ


    ๋‹ค์Œ ์˜ˆ์ œ์™€ ๊ฐ™์ด ์‚ผ๊ฐํ˜• ๋ชจ์–‘์œผ๋กœ ‘*’์„ ์ถœ๋ ฅํ•œ๋‹ค.(๊ณต๋ฐฑ์˜ ๊ฐœ์ˆ˜์™€ ๋ณ„์˜ ๊ฐœ์ˆ˜๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ํ™•์ธํ•ด์ฃผ์‹œ๋ฐ”๋ž๋‹ˆ๋‹ค.)

     

     

     

    ์˜ˆ์ œ์ž…๋ ฅ

    3

     

    ์˜ˆ์ œ์ถœ๋ ฅ

      *
     ***
    *****

     

     

     

     

    ๋‚ด๊ฐ€ ํ•ด๊ฒฐํ•œ ๋ฐฉ์‹

     

      	//๋ช‡์ธต
            int num;
    
    	//์ž…๋ ฅ๋ฐ›๊ธฐ
            Scanner sc = new Scanner(System.in);
            num = sc.nextInt();
            
            //์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์— ๋Œ€ํ•ด์„œ ๊ฐ€๋กœ๋กœ ๋ช‡์ค„ ๊ทธ๋ฆด๊ฒƒ์ธ์ง€ ๊ณ„์‚ฐ
            // 3์ธต์ด๋ฉด -> ํ•œ ํ–‰์— ๋Œ€ํ•ด์„œ ์ด 5๊ฐœ๋ฅผ ๊ทธ๋ฆฐ๋‹ค.
            int row = num*2-1;
            
            //์ธต๋ณ„๋กœ ๋Œ์•„๋ณธ๋‹ค.
            for(int i = 1; i <= num; i++ ) {
            
            	//์—ฌ๊ธฐ์„œ empty๋Š” ๊ณต๋ฐฑ์„ ์–ผ๋งˆ๋‚˜ ๊ทธ๋ฆด๊ฒƒ์ธ๊ฐ€์ด๋‹ค.
                    //1์ธต์ผ๋•Œ -> 1๊ฐœ์˜ ๊ณต๋ฐฑ : 2*1-1 = 1 -> 5 - 1 = 4
                    //2์ธต์ผ๋•Œ -> 2๊ฐœ์˜ ๊ณต๋ฐฑ : 2*2-1 = 3 -> 5 - 3 = 2
                    //3์ธต์ผ๋•Œ -> 0๊ฐœ์˜ ๊ณต๋ฐฑ : 2*3-1 = 5 -> 5 - 5 = 0
                    //์—ฌ๊ธฐ์„œ ๋‚˜๋ˆ„๊ธฐ 2๋ฅผ ํ•œ ์ด์œ ๋Š” ์•ž๋’ค๋กœ ๊ณต๋ฐฑ์ด ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์—
                    //๋ฐ˜๋ฐ˜์”ฉ ๊ทธ๋ฆฌ๊ธฐ ์œ„ํ•จ์ด๋‹ค.
                    int empty = (row - (2*i-1)) / 2;
                    
                    //๋จผ์ € ์•ž์ชฝ ๊ณต๋ฐฑ์„ ๊ทธ๋ฆฐ๋‹ค.
                    for(int k = 0; k < empty; k++) {System.out.print(" ");}
                    
                    //๋‹ค์Œ์œผ๋กœ ์ค‘๊ฐ„์— *์„ ์ฐ์–ด์ค€๋‹ค. 
                    //์ „์ฒด - ์ „์ฒด๊ณต๋ฐฑ = ๋ณ„์˜ ๊ฐฏ์ˆ˜
                    for(int k = 0; k < row-(empty*2); k++) {System.out.print("*");}
                    
                    //๋งˆ์ง€๋ง‰์œผ๋กœ ๋‚จ์€ ๊ณต๋ฐฑ์„ ๊ทธ๋ฆฐ๋‹ค.          
                    for(int k = 0; k < empty; k++) {System.out.print(" ");}
                    
                    //ํ•œ์ค„ ๋‹ค ๊ทธ๋ ธ์œผ๋‹ˆ, ๋‹ค์Œ ์ค„๋กœ ๋„˜๊ธด๋‹ค.
                    System.out.print("\n");
            }โ€‹

    ๋Œ“๊ธ€

Designed by Tistory.