*solved.ac 기준 브론즈 3단계 문제
풀이)
그러나 이러한 풀이는 약수를 모두 구해야 하기 때문에 시간이 필연적으로 오래 걸린다.
내 코드)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[]args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String[] input= bf.readLine().split(" ");
int big = Integer.parseInt(input[0]);
int small = Integer.parseInt(input[1]);
String str = "";
for(int i = 1;i<=big/2; i++) {
if (big % i == 0) {
str = str + String.valueOf(i) + " ";
}
else
continue;
}
str += String.valueOf(big);
String strArr[] = str.split(" ");
if(strArr.length < small)
System.out.println(0);
else
System.out.println(strArr[small-1]);
}
}
다른 풀이)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[]args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String[] input= bf.readLine().split(" ");
int big = Integer.parseInt(input[0]);
int small = Integer.parseInt(input[1]);
int count = 0;
for(int i = 1;i<=big;i++) {
if (big % i == 0) {
count ++;
if(count == small) {
System.out.println(i);
break;
}
}
}
if(count < small)
System.out.println(0);
}
}