7 solutions
-
4
#include<bits/stdc++.h> using namespace std; long n; long s=0;//计数变量 bool ss(long x){ for(int i=2;i<=x/2;i++){//i从2开始不然会全true if(x%i==0) return false; } return true; }//判断素数函数 int main(){ cin>>n; for(int i=2;i<=n;i++) if(ss(i)) s++; cout<<s; return 0; }//说实话真可以不用函数
-
1
#include <bits/stdc++.h> using namespace std; int cnt; bool su(int n); int main(int argc, char **argv){ int n; cin >> n; for (int i = 2;i <= n;i++){ if (su(i)){ cnt++; } } cout << cnt; return 0; } bool su(int n){ for (int i = 2;i < n;i++){ if (n % i == 0){ return false; } } return true; }
dstyg
-
0
#include<bits/stdc++.h> using namespace std; bool sushu(int n){//判断n是否为质数 if(n<2) return false;//如果n<2 返回假 for(int i=2;i<=sqrt(n);i++){ if(n%i==0) return false;//如果n%i余数是0,说明n不是质数,返回假 } return true;//排除前两项,说明它是质数,返回真 } int main(){ int n; cin>>n; int sum=0; for(int i=2;i<=n;i++) if(sushu(i)) sum++; cout<<sum; return 0; }
-
0
#include<bits/stdc++.h> using namespace std; long long n, num=0; bool sushu(long long x) { for(int i=2;i<=x/2;i++) { if(x%i==0)//能被除本身和1整除的数不是素数 { return false; } } return true; } int main(){ cin>>n; for(int i=2;i<=n;i++) { if(sushu(i)==true)//判断 { num++; } } cout<<num;//输出 return 0; } //双重循环不是最优解
- 1
Information
- ID
- 637
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 3
- Tags
- # Submissions
- 104
- Accepted
- 53
- Uploaded By