Algorithm๐Ÿฅ‡

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

hae02y 2023. 10. 23. 23:15
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

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

## ์ž…๋ ฅ

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

## ์ถœ๋ ฅ

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

์‹œ๊ฐ„ ์ œํ•œ ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ ์ œ์ถœ ์ •๋‹ต ๋งžํžŒ ์‚ฌ๋žŒ ์ •๋‹ต ๋น„์œจ
1 ์ดˆ 256 MB 99599 54229 42046 53.083%

https://www.acmicpc.net/problem/11653

ํ’€์ด

๋กœ์ง์„ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ’€์ด๋ ์ˆ˜์žˆ๋‹ค.

72 / 2 = 36;
36 / 2 = 18;
18 / 2 = 9;
9 / 3 = 3;
3 / 3= 1;
1 < 2; (break);

72๋ฅผ ๋‚˜๋ˆˆ๋‹ค๊ณ  ํ–ˆ์„๋•Œ ๋‚˜๋ˆ ์ง€๋Š” ๊ฐ’์œผ๋กœ ๊ณ„์†ํ•ด์„œ ๋‚˜๋ˆˆ๋‹ค.

์ฝ”๋“œ


import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStreamReader;  
import java.util.LinkedList;  
import java.util.List;  

public class ์†Œ์ธ์ˆ˜๋ถ„ํ•ด {  

    public static void main(String[] args) throws IOException {  
        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));  

        int n = Integer.parseInt(br.readLine());  

        List<Integer> list = new LinkedList<>();  

        while (true){  
            if(n < 2) break;  //n์ด 2๋ณด๋‹ค ์ž‘์„๋•Œ while๋ฌธ ํƒˆ์ถœ

            for(int i=2; i<=n; i++){  
                if(n%i == 0){  //n์ด i๋กœ ๋‚˜๋ˆ ์งˆ๋•Œ
                    list.add(i);  //๋‚˜๋ˆ ์ง€๋Š” ๊ฐ’์€ ์†Œ์ˆ˜์ด๋ฏ€๋กœ, list์— ๋„ฃ์–ด์ค€๋‹ค.
                    n /= i;  //n์„ i๋กœ ๋‚˜๋ˆ„๊ณ  for๋ฌธ์„ ํƒˆ์ถœํ•œ๋‹ค.
                    break;  
                }  
            }  
        }  
        list.forEach(System.out::println);  
        br.close();  
    }  
}
๋ฐ˜์‘ํ˜•