[JAVA/2501번] 약수 구하기

고지훈·2021년 12월 9일
1

Algorithm

목록 보기
52/68
post-thumbnail

문제


입력 및 출력


풀이

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

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

    // N, K
    String[] input = br.readLine().split(" ");
    int N = Integer.parseInt(input[0]);
    int K = Integer.parseInt(input[1]);

    // 약수를 담을 배열
    int[] array = new int[N+1];

    // 배열의 인덱스
    int index = 0;
    
    // N만큼 반복문을 수행
    for(int i = 1; i <= N; i++) {
      if(N%i == 0) {
        array[index] = i;
        index++;
      }
    }

    // K번째로 작은 약수를 출력
    System.out.println(array[K-1]);
  }
}

결과 및 해결방법

[결과]

profile
"계획에 따르기보다 변화에 대응하기를"

0개의 댓글