非官方题解
2025-03-16 22:15:36
发布于:北京
19阅读
0回复
0点赞
由于这道题的数据比较小,暴力是可以过的。
首先,我们写一个素数判断的函数:
bool is_prime (int x)
{
if (x <= 1) return false;
for (int i = 2; pow (i, 2) <= x; i ++)
{
if (x % i == 0) return false;
}
return true;
}
注意:for循环中要写小于等于,因为有完全平方数。
接下来,加上“q次询问”。找最大的,所以注意要倒着找。
Code:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <random>
#define endl "\n"
#define Ll long long
#define true 1
#define false 0
using namespace std;
const int N = 1e5 + 10, M = 1e2 + 10;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
Ll T = 1;
int q, n;
bool is_prime (int x)
{
if (x <= 1) return false;
for (int i = 2; pow (i, 2) <= x; i ++)
{
if (x % i == 0) return false;
}
return true;
}void solve ()
{
//main;
cin >> q;
while (q --)
{
cin >> n;
for (int i = -- n; i >= 2; -- i)
if (is_prime (i))
{
cout << i << endl;
break;
}
}
return ;
}int main (int argc, const char * argv[])
{
//main;
//cin >> T;
while (T --)
{
solve ();
}return 0;
}
这里空空如也
有帮助,赞一个