因数统计,筛选质数
2026-05-23 14:52:52
发布于:湖北
30阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
bool check(int x){
int m = x,sum = 0;
for(int i=2;i<=sqrt(m);i++){
if(x%i==0){
sum++;
while(x%i==0) x/=i; //反复去除,这样都是质数
}
}
//跑完了,剩下不是1就是另外一个因数 比如15的因数 5
if(x>1) sum++;
return sum==2;//看看是不是刚好2个
}
int main(){
int q;
cin>>q;
while(q--){
int x;
cin>>x;
if(check(x)) cout<<1<<endl;
else cout<<0<<endl;
}
return 0;
}
这里空空如也



有帮助,赞一个