$1$ 不是质数,但是您的代码并没有特判
by endswitch @ 2023-04-26 20:10:21
@[gao3051319126](/user/967545)
by endswitch @ 2023-04-26 20:10:31
这样:
```cpp
#include<stdio.h>
#include<math.h>
bool zhishu(int i)//质数判断
{ if(i<=1)return 0;
for(int j=2;j<=sqrt(i);j++)
{
if(i%j==0)
{
return 0;
}
}
return 1;
}
int main()
{
int a[105],n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++)
{
if(zhishu(a[i])==1)//是质数
{
printf("%d ",a[i]);
}
}
return 0;
}
```
by __My0217__ @ 2023-04-26 20:13:10
这样也可以:
```cpp
#include<bits/stdc++.h>
using namespace std;
int a[1000005];
bool prime(int h){
if(h<=1) return 0;
for(int i=2;i<=h-1;i++){
if(h%i==0){
return 0;
}
}
return 1;
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
if(prime(a[i])==1){
cout<<a[i]<<" ";
}
}
return 0;
}
```
~~你真的以为1是质数吗,一只有一个因数诶~~
by Razer_System @ 2023-04-26 20:41:13
还有一个小小的建议:i尽量不要当做变量名或数组名,i一般都用于循环变量
by Razer_System @ 2023-04-26 20:42:43