求助
查看原帖
求助
1307331
yayaPengRuiYang楼主2024/11/13 21:52

#include<bits/stdc++.h> using namespace std; #define maxp 65536 #define ll long long

int primes[maxp]; bool notprime[maxp];

int Eratosthenes(int n) { notprime[1] = true; // 1不是素数 int cnt = 0; for (int i = 2; i <= n; i++) { if (!notprime[i]) { primes[cnt++] = i; // 将素数i添加到primes数组

        // 标记i的倍数为非素数
        for (ll j = (ll)i * i; j <= n; j += i) {
            notprime[j] = true;
        }
    }
}
return cnt; // 返回素数的数量

}

int main() { int n; cin >> n; int cnt = Eratosthenes(n); // 调用筛选函数并获取素数的数量

// 输出2到n之间的素数个数
cout << cnt << endl;

return 0;

}//有点核弹打蚊子的感觉,而且关键是还错了:(```cpp #include<bits/stdc++.h> using namespace std; #define maxp 65536 #define ll long long

int primes[maxp]; bool notprime[maxp];

int Eratosthenes(int n) { notprime[1] = true; // 1不是素数 int cnt = 0; for (int i = 2; i <= n; i++) { if (!notprime[i]) { primes[cnt++] = i; // 将素数i添加到primes数组

        // 标记i的倍数为非素数
        for (ll j = (ll)i * i; j <= n; j += i) {
            notprime[j] = true;
        }
    }
}
return cnt; // 返回素数的数量

}

int main() { int n; cin >> n; int cnt = Eratosthenes(n); // 调用筛选函数并获取素数的数量

// 输出2到n之间的素数个数
cout << cnt << endl;

return 0;

}

2024/11/13 21:52
加载中...