Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 第一種(網路)
- #include <iostream>
- #include <cmath>
- using namespace std ;
- int main() {
- int a,b;
- while(cin>>a>>b){
- int sum=0;
- for(int i=a; i<=b; i++){
- int flag=1;
- for(int j=2; j<=sqrt(i); j+=(j==2?1:2))
- if(i%j==0) {flag=0; break;}
- if(i!=1&&flag==1) sum++;
- }
- cout<<sum<<endl;
- }
- return 0;
- }
- 第二種(我)
- #include<iostream>
- using namespace std;
- int main()
- {
- int a,b,count;
- bool find;
- while(cin>>a>>b){
- count=(a<=2?1:0);
- if(b>a)
- swap(a,b);
- if(a%2==0)
- ++a;
- else if(a==1)
- a+=2;
- for(;a<=b;a+=2){
- find=true;
- for(int j=3;j*j<=a;j+=2)
- if(a%j==0){
- find=false;
- break;
- }
- if(find==true){
- ++count;
- }
- }
- cout<<count<<"\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement