Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. 第一種(網路)
  2. #include <iostream>
  3. #include <cmath>
  4. using namespace std ;
  5. int main() {
  6. int a,b;
  7. while(cin>>a>>b){
  8. int sum=0;
  9. for(int i=a; i<=b; i++){
  10. int flag=1;
  11. for(int j=2; j<=sqrt(i); j+=(j==2?1:2))
  12. if(i%j==0) {flag=0; break;}
  13. if(i!=1&&flag==1) sum++;
  14. }
  15. cout<<sum<<endl;
  16. }
  17. return 0;
  18. }
  19. 第二種(我)
  20. #include<iostream>
  21. using namespace std;
  22.  
  23. int main()
  24. {
  25. int a,b,count;
  26. bool find;
  27. while(cin>>a>>b){
  28. count=(a<=2?1:0);
  29. if(a%2==0)
  30. ++a;
  31. else if(a==1)
  32. a+=2;
  33. for(;a<=b;a+=2){
  34. find=true;
  35. for(int j=3;j*j<=a;j+=2)
  36. if(a%j==0){
  37. find=false;
  38. break;
  39. }
  40. if(find==true){
  41. ++count;
  42. }
  43. }
  44. cout<<count<<"\n";
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement