본문 바로가기
C#/백준 알고리즘

2022.01.27 [백준] C# 소인수분해

by ian's coding 2022. 1. 27.
728x90
반응형

 

 

 

 

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

 

11653번: 소인수분해

첫째 줄에 정수 N (1 ≤ N ≤ 10,000,000)이 주어진다.

www.acmicpc.net

 

 

 

 

 


풀이 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System;
using System.Text;
class Program{
    static void Main(){
        StringBuilder sb = new StringBuilder();
        int n = int.Parse(Console.ReadLine());
        for(int i=2;i<=n;i++){
            while(n%i==0){
                sb.Append(i+"\n");
                n/=i;
            }
        }
        Console.WriteLine(sb);
    }
}
cs

 입력받은 값 n을 for문을 이용해 2부터 나눠지지 않을때까지 나눠줌을 반복한다. 그래서 2로 더 이상 나눠지지 않으면 3으로 넘어가서 나눠줌을 반복한다. 

 

 

 

 

728x90
반응형

댓글