题解 | #Prime Number#
Prime Number
https://www.nowcoder.com/practice/c5f8688cea8a4a9a88edbd67d1358415
笨办法,本来想用一维数组记录标记素数的办法确定素数集合的,但是最大范围不好确定
#include <iostream> #include "cmath" #include "vector" using namespace std; bool isPrime(int a) { int bound = sqrt(a); for (int i = 2; i <= bound; i++) { if (a % i == 0) return false; } return true; } int main() { vector<int> primes; int num = 2; while (primes.size() < 10000) { if (isPrime(num)) primes.push_back(num); num++; } int n; while (cin >> n) { // 注意 while 处理多个 case // cout << a + b << endl; cout << primes[n-1] << endl; } } // 64 位输出请用 printf("%lld")