1492.cpp (339B)
1 class Solution { 2 public: 3 int kthFactor(int n, int k) { 4 int d = 1; 5 for (; d * d <= n; d++) { 6 if (n % d == 0 && --k == 0) return d; 7 } 8 9 d--; 10 if (d * d == n) d--; 11 for (; d >= 1; d--) { 12 if (n % d == 0 && --k == 0) return n / d; 13 } 14 return -1; 15 } 16 };