解题思路:由找素数代码演进而来
#include<algorithm> #include<cstdio> #include<iostream> #include<math.h> using namespace std; typedef long long LL; bool isprime(LL num) { if (num < 2) return false; for (int i = 2; i > start >> end; if (end < 2) cout << 0 << endl; else { if (start < 2) start = 2; LL limits = (LL)sqrt((double)end);//开根号 for (LL i = 2; i 2 ? start / i : 2;//i从2开始增大,为了使(N*i从start开始,减少多余计算)(若start<=4时取2,不影响对素数判断) for (; N*i = start) primetable[N*i - start] = true;//合数一定能由一个素数和另一个数相乘得到 }//所以N*i是合数,N*i - start是其所在区间位置 int total = 0; for (LL i = 0; i <= end - start; i++) if (!primetable[i])//为false时素数 total++; cout << total << endl; } }
注意事项:
参考代码:
#include<iostream> #include<math.h> using namespace std; typedef long long LL; bool s(LL i) { int t=0; if(i<2) return false; else for(t=2;t<=sqrt((double)i);t++) if(i%t==0) return false; return true; } bool num[2000000]={false}; int main() { LL end,i=0,N=0,p=0; cin>>end; p=sqrt((double)end); for(i=2;i<=p;i++) { if(s(i)) for(N=2;N*i<=end;N++) num[N*i-2]=true; } LL total=0; for(i=2;i<=end;i++) if(!num[i-2]) total=total+i; cout<<total<<endl; }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:375 |
校门外的树 (C语言代码)浏览:961 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:475 |
简单的a+b (C语言代码)浏览:528 |
三角形 (C语言代码)浏览:914 |
C语言程序设计教程(第三版)课后习题11.1 (C语言代码)浏览:621 |
A+B for Input-Output Practice (III) (C语言代码)浏览:569 |
Tom数 (C语言代码)浏览:495 |
C语言程序设计教程(第三版)课后习题11.3 (C语言代码)浏览:576 |
The 3n + 1 problem (C语言代码)浏览:505 |