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

[BOJ/Step9] 11653 : ์†Œ์ธ์ˆ˜๋ถ„ํ•ด (JAVA)

NaNaRin๐Ÿ™ƒ 2021. 1. 19. 16:11

www.acmicpc.net/problem/11653

 

11653๋ฒˆ: ์†Œ์ธ์ˆ˜๋ถ„ํ•ด

์ฒซ์งธ ์ค„์— ์ •์ˆ˜ N (1 ≤ N ≤ 10,000,000)์ด ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net


๋ฌธ์ œ

์ •์ˆ˜ N์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์†Œ์ธ์ˆ˜๋ถ„ํ•ดํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •์ˆ˜ N (1 ≤ N ≤ 10,000,000)์ด ์ฃผ์–ด์ง„๋‹ค.

 

์ถœ๋ ฅ

N์˜ ์†Œ์ธ์ˆ˜๋ถ„ํ•ด ๊ฒฐ๊ณผ๋ฅผ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. N์ด 1์ธ ๊ฒฝ์šฐ ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ 1

72

 

์˜ˆ์ œ ์ถœ๋ ฅ 1

2

2

2

3

3

 

์˜ˆ์ œ ์ž…๋ ฅ 2

3

 

์˜ˆ์ œ ์ถœ๋ ฅ 2

3

 

์˜ˆ์ œ ์ž…๋ ฅ 3

6

 

์˜ˆ์ œ ์ถœ๋ ฅ 3

2

3

 

์˜ˆ์ œ ์ž…๋ ฅ 4

2

 

์˜ˆ์ œ ์ถœ๋ ฅ 4

2

 

์˜ˆ์ œ ์ž…๋ ฅ 5

9991

 

์˜ˆ์ œ ์ถœ๋ ฅ 5

97

103


ํ’€์ด

(๊ฒฝํ—˜๋‹ด : ์ „๊ณผ ๊ฐ™์ด ๋ฏธ๋ฆฌ ์†Œ์ˆ˜๋ฅผ ๊ตฌํ•ด ๋ฐฐ์—ด์— ๋„ฃ์–ด๋‘๋ ค๊ณ  ํ•˜๋ฉด ์™„์ „ ๋งํ•œ๋‹ค...)

1. ์ž…๋ ฅ๋ฐ›์€ n์„ a๋กœ ๋‚˜๋ˆ ์„œ ๋‚˜๋จธ์ง€๊ฐ€ 0์ผ๋•Œ a๋ฅผ ๋ฒ„ํผ๋กœ ์ถœ๋ ฅํ•˜๊ณ , n์„ a๋กœ ๋‚˜๋ˆ ์ค€๋‹ค. a๋Š” 2๋ถ€ํ„ฐ

2. n์ด ๋”์ด์ƒ a๋กœ ๋‚˜๋ˆ ์ง€์ง€ ์•Š์œผ๋ฉด a++

3. n์ด 1์ผ๋•Œ ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•˜๊ณ  ๋ฒ„ํผ๋ฅผ ์ถœ๋ ฅ

 

import java.io.*;
import java.util.Scanner;

public class B11653 {

	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 n = Integer.parseInt(br.readLine());
		int a = 2;	
		
		while(n != 1) {
			if(n % a == 0) {
				n /= a;
				bw.write(a + "\n");
			} else {
				a++;
			}
		}
		bw.flush();
	}
}