본문 바로가기

Algorithm/Baekjoon

[백준_JAVA] 2501 약수 구하기

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		int count = 0;
		
		for(int i = 1; i <= N; i++) {
			if(N % i == 0)
				count++;
			
			if(count == K) {
				System.out.println(i);
				break;
			}
		}
		
		if(count < K)
			System.out.println(0);
		sc.close();
			
	}
}

배열에 모든 약수의 값과 0을 담으려고 했지만 arraylist를 사용해야 할 것 같아서 하지 않았다