SHARE
TWEET

Untitled

a guest Jan 20th, 2020 108 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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(b>a)
  30.             swap(a,b);
  31.         if(a%2==0)
  32.             ++a;
  33.         else if(a==1)
  34.             a+=2;
  35.         for(;a<=b;a+=2){
  36.             find=true;
  37.             for(int j=3;j*j<=a;j+=2)
  38.                 if(a%j==0){
  39.                     find=false;
  40.                     break;
  41.                 }
  42.             if(find==true){
  43.                 ++count;
  44.             }
  45.         }
  46.         cout<<count<<"\n";
  47.     }
  48. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top