c++ 萌新求助:怎么判断质数啊?
查看原帖
c++ 萌新求助:怎么判断质数啊?
530225
luogu530225楼主2021/7/15 14:12
#include<iostream>
#include<cmath>
using namespace std;
int main(){
    double l,he=0,lhe,lheguo,num;
    int y;
	cin>>l;
	lhe=(1+l)*l/2;
	if(l==1){
		cout<<"0";
	}else if(l==2){
		cout<<"2"<<endl<<"1";
	}else if(l==3){
	cout<<"2"<<endl<<"3"<<endl<<"2";
	}else{
		cout<<"2"<<endl<<"3"<<endl;
		num=2;
		he=5;
 for(double i=4;he<l;i++){//被判断质数的数 
for(double x=1;x<=sqrt(i);x++){//用来判断质数的因数 
	    	lheguo=lhe;
	    	y=i/x;
			    if(y/1!=i/x){
	   lhe-=i;//判断是否是质数 
			        x=1;
			        break;
				}		
		    }
		    if(lhe!=lheguo){
			cout<<i<<endl;
				he+=i;
		        	num+=1;
			}
	    }
	    cout<<num;
    return 0;
    }
}
2021/7/15 14:12
加载中...