top1编程
← 返回题目
题解

【基础】素数个数

1 条题解

  • 0
    @ 2026-7-28 22:44:53
    #include <bits/stdc++.h>//include引用 包含,i:input输入 o:output输出
    using namespace std;
    bool ss(int n){
    	bool f=true;
    	for(int i=2;i<=sqrt(n);i++){
    		if(n%i==0){
    			f=false;
    			break;
    		}
    	}
    	if(n>1&&f){
    		return true;
    	}
    	else{
    		return false;
    	}
    }
    int main()
    {
    	int s=0,n;
        cin>>n;
    	for(int i=2;i<=n;i++){
    		if(ss(i)){
    			s++;
    		}
    	}
    	cout<<s;
    	return 0;
    }
    
    • 1