문제
https://www.acmicpc.net/problem/2501
자연수 N과 K가 주어졌을 때 N의 약수들 중에서 K번째로 작은 수를 출력하는 문제였다.
문제 풀이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer t = new StringTokenizer(br.readLine(), " ");
int N=Integer.parseInt(t.nextToken());
int K=Integer.parseInt(t.nextToken());
int count=0; //약수 개수
for(int i=1;i<=N;i++) {
if(N%i==0) {
count++; //개수 증가
if(count==K) { //개수(순서)가 K 일 때 i 출력
System.out.println(i);
return;
}
}
}
System.out.println(0);
}
}
'코딩테스트 > 백준' 카테고리의 다른 글
[백준/JAVA]1978번 소수 찾기 (0) | 2025.05.07 |
---|---|
[백준/JAVA]9506번 약수들의 합 (0) | 2025.05.06 |
[백준/JAVA]5086번 배수와 약수 (0) | 2025.05.05 |
[백준/JAVA]2869번 달팽이는 올라가고 싶다 (0) | 2025.05.04 |
[백준/JAVA]2292번 벌집 (0) | 2025.03.22 |