์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Java

[BOJ/Step3] 2439 : ๋ณ„ ์ฐ๊ธฐ - 2 (JAVA)

NaNaRin๐Ÿ™ƒ 2021. 1. 7. 21:37

www.acmicpc.net/problem/2439

 

2439๋ฒˆ: ๋ณ„ ์ฐ๊ธฐ - 2

์ฒซ์งธ ์ค„์—๋Š” ๋ณ„ 1๊ฐœ, ๋‘˜์งธ ์ค„์—๋Š” ๋ณ„ 2๊ฐœ, N๋ฒˆ์งธ ์ค„์—๋Š” ๋ณ„ N๊ฐœ๋ฅผ ์ฐ๋Š” ๋ฌธ์ œ ํ•˜์ง€๋งŒ, ์˜ค๋ฅธ์ชฝ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ ๋ณ„(์˜ˆ์ œ ์ฐธ๊ณ )์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

www.acmicpc.net


๋ฌธ์ œ

์ฒซ์งธ ์ค„์—๋Š” ๋ณ„ 1๊ฐœ, ๋‘˜์งธ ์ค„์—๋Š” ๋ณ„ 2๊ฐœ, N๋ฒˆ์งธ ์ค„์—๋Š” ๋ณ„ N๊ฐœ๋ฅผ ์ฐ๋Š” ๋ฌธ์ œ

ํ•˜์ง€๋งŒ, ์˜ค๋ฅธ์ชฝ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ ๋ณ„(์˜ˆ์ œ ์ฐธ๊ณ )์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N(1 ≤ N ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค.

 

์ถœ๋ ฅ

์ฒซ์งธ ์ค„๋ถ€ํ„ฐ N๋ฒˆ์งธ ์ค„๊นŒ์ง€ ์ฐจ๋ก€๋Œ€๋กœ ๋ณ„์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ

5

 

์˜ˆ์ œ ์ถœ๋ ฅ 

        *

      **

    ***

  ****

*****


ํ’€์ด

1. *์„ ์ถœ๋ ฅํ•  ํšŸ์ˆ˜ num์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

2. 1๋ฒˆ์งธ ์ค„์— ๊ณต๋ฐฑ num-1๊ฐœ์™€ * 1๊ฐœ, 2๋ฒˆ์งธ ์ค„์— ๊ณต๋ฐฑ num-2๊ฐœ์™€ * 2๊ฐœ … i๋ฒˆ์งธ ์ค„์— ๊ณต๋ฐฑ num-i๊ฐœ์™€ * i๊ฐœ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

3. ๋”ฐ๋ผ์„œ ๊ณต๋ฐฑ์€ ๋ณ€์ˆ˜ k๊ฐ€ 0๋ถ€ํ„ฐ num-i๊นŒ์ง€ ์ถœ๋ ฅ, *์€ ๋ณ€์ˆ˜ j๊ฐ€ 0๋ถ€ํ„ฐ i๊นŒ์ง€ ์ถœ๋ ฅ๋˜์–ด์•ผ ํ•œ๋‹ค. (i๋Š” i๋ฒˆ์งธ ์ค„. 1๋ถ€ํ„ฐ num๊นŒ์ง€ ์ฆ๊ฐ€)

    => โ€ป ์ฃผ์˜ โ€ป i๋ฅผ for(int i = 0; i < num; i++) ๋กœ ์„ค์ •ํ•˜๋ฉด ๊ณต๋ฐฑ์ด num๊ฐœ *์ด 0๊ฐœ ์ถœ๋ ฅ๋˜์–ด

         ์ฒซ๋ฒˆ์งธ ์ค„์ด ๊ณต๋ฐฑ์ด ๋˜๋ฏ€๋กœ for(int i = 1; i <= num; i++) ๋กœ ์„ค์ •ํ•œ๋‹ค.

 

package step3;

import java.io.*;

public class B2439 {

	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		int num = Integer.parseInt(br.readLine());
		
		for(int i = 1; i <= num; i++) {
			
			for(int k = 1 ; k <= num - i; k++) {
				bw.write(" ");
			}
			
			for(int j = 1; j <= i; j++) {
				bw.write("*");
			}
			bw.write("\n");
		}
		
		bw.flush();
		bw.close();

	}
}