判断一下1不是质数
by styl0224 @ 2023-12-10 11:14:31
@[chengwenhao](/user/1184968)
if(a%i==0||a==1)改为 if(a%i==0||a!=1)
by 40tianlingying @ 2023-12-12 20:06:18
```
#include<bits/stdc++.h>
using namespace std;
int sushu(int a)
{
int flag=1;
if(a<2)
{
return 0;
}
for(int i=2;i<=sqrt(a);i++)
{
if(a%i==0)
{
flag=0;
break;
}
}
return flag;
}
int main()
{
int a[105];
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
if((sushu(a[i]))==1)
{
cout<<a[i]<<" ";
}
}
return 0;
}
```
by lizhengfei0824 @ 2024-01-16 13:50:28
如果a == 1,代码根本就不会访问循环里的判断。
所以你应该把对1的判断放在外面。
贴代码:
```cpp
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
bool zhishu(int x)
{
if(x == 1)
{
return 1 > 2;
}
if(x == 2)
{
return 1 < 2;
}
if(x % 2 == 0)
{
return 1 > 2;
}
for(int i = 3; i <= sqrt(x); i += 2)
{
if(x % i == 0)
{
return 1 > 2;
}
}
return 1 < 2;
}
int main()
{
int a[105], n;
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
if(zhishu(a[i]))
{
cout << a[i] << " ";
}
}
return 0;
}
```
by Hamdi @ 2024-02-03 10:33:24
//我懒得写true和false了
by Hamdi @ 2024-02-03 10:36:06