考察大家对于质数的判断:
#include <bits/stdc++.h>
using namespace std;
bool isPrime(int x)
{
if (x == 1)
return false; // 1 既不是质数也不是合数
for (int i = 2; i * i <= x; i++)
if (x % i == 0)
return false; // 有除了 1 和自身以外的因子的数不是质数
return true; // 其余的数都是质数
}
void solve()
{
int n;
cin >> n;
bool flag = false;
for (int i = 2; i <= n; i++)
{
if (isPrime(i))
{
if (flag)
cout << " ";
cout << i;
flag = true;
}
}
cout << endl;
}
signed main()
{
ios::sync_with_stdio(false);
int t;
// cin >> t;
t = 1;
while (t--)
solve();
return 0;
}