#include <iostream> #include <cmath> using namespace std; // 判断一个数是否为素数的函数 bool isPrime(int num) { if (num < 2) return false; for (int i = 2; i <= sqrt(num); i++) { if (num % i == 0) return false; } return true; } int main() { int n; cin >> n; int maxLength = 0; int currentLength = 0; // 遍历从 1 到 n 的每个数 for (int i = 1; i <= n; i++) { if (!isPrime(i)) { // 如果当前数是非素数,连续非素数长度加 1 currentLength++; if (currentLength > maxLength) { // 更新最大连续非素数长度 maxLength = currentLength; } } else { // 如果当前数是素数,连续非素数长度重置为 0 currentLength = 0; } } cout << maxLength << endl; return 0; } /************************************************************** Problem: 1587 User: linmiaoling Language: C++ Result: Accepted Time:289 ms Memory:2072 kb ****************************************************************/