每天OnlineJudge之 “数素数”

来源:互联网 发布:ui和程序员漫画 编辑:程序博客网 时间:2024/04/30 08:11

Problem
素数是的只能被1和它本身整除的自然数。判断一个数是素数的方法是使用2到该数的平方根的素数除它,若有能整除的则该数不是素数。

Input
本题有多组数据,每组数据由两个正整数M,N组成。(0<M<N<1000000)

Output
输出一个整数,表示介于M,N之间(包括M,N)的素数的数量。

Sample Input
5 10
1 3
6 8

Sample Output
2
2
1

---------------------------
第一次尝试,不合乎要求的代码

/*
此解答未Accepted
原因:内存使用超出限制
Memory Limit Exceeded 4100k 25ms C++ 2006-05-07 22:56:07 
*/


#include
<stdio.h>
#include
<math.h>
#include
<malloc.h>

int* arrIs;
int count=0;
int start=0;
int end=0;

int Is(int n)
{
    
if(arrIs[n] != -1)
    
{
        
return arrIs[n];
    }


    
if(n>3)
    
{
        
for(int i=2; i<=sqrt(n); i++)
        
{
            
if( Is(i) && n%i==0 )
            
{
                arrIs[n] 
= 0;
                
return 0;
            }

        
        }

    }


    arrIs[n]
=1;
    
return 1;
}


int main()
{

    
while(scanf("%d",&start)!=EOF && scanf("%d",&end)!=EOF)
    
{
        count 
=0;

        arrIs 
= (int*)malloc((end+1)*4);

        
for(int i=0; i<=end; i++)
        
{
            arrIs[i] 
= -1;
        }


        
for(int j=start; j<=end; j++)
        
{
            
if(Is(j))
                count
++;
        }


        printf(
"%d",count);
        
    }


    
return 0;
}






--------------------------------------------------------------


第二次尝试,用时间换空间,但仍是不合乎要求的

/*
此解答未Accepted
原因:时间使用超出限制
 
 Result              Memory Time   Language Date  
 Time Limit Exceeded 192k   2000ms C++      006-05-08 18:11:05 
 
*/



#include
<stdio.h>
#include
<math.h>
#include
<malloc.h>


typedef 
struct node_
{
    
int num;
    node_ 
* next;
}
node;


node
* nodesHead;
int count=0;
int start=0;
int end=0;


int Is(int);
int Find(int);



int Is(int n)
{
    
if(Find(n))
    
{
        
return 1;
    }

    
if(n>3)
    
{
        
for(int i=2; i<=sqrt(n); i++)
        
{
            
if( Is(i) && n%i==0 )
            
{
                
return 0;
            }

        
        }

    }
else if(n<=1)
    
{
        
return 0;
    }


    node 
* nd = (node*)malloc(sizeof(node));
    nd
->num = n;
    nd
->next = nodesHead->next;
    nodesHead
->next = nd;

    
return 1;
}



int Find(int n)
{
    node
* h = nodesHead;
    
while(h->next)
    
{
        
if(h->num == n)
        
{
            
return 1;
        }

        h
=h->next;
    }

    

    
return 0;
}



int main()
{
    nodesHead 
= (node*)malloc(sizeof(node));
    nodesHead
->num=2;
    nodesHead
->next=0;
    
while(scanf("%d",&start)!=EOF && scanf("%d",&end)!=EOF)
    
{
        count 
=0;

        
for(int j=start; j<=end; j++)
        
{
            
if(Is(j))
                count
++;
        }


        printf(
"%d/n",count);
        
    }



    
return 0;
}


最终的解答,正确的

#include<iostream> 
#include
<cmath> 
using namespace std;
int isprime(int); 

int main()
{
    
int m,n;int i,j;int number=0int primenum=0
    
int a[170]; 
    
for(i=2;i<=1000;i++)
    

        
if(isprime(i))
            a[primenum
++]=i;
    }
//先作1--1000素数表 
   
    
while(scanf("%d %d",&n,&m)!=EOF)
    
{
         
for(i=n;i<=m;i++)//i从n测试到m 
         {  
             
if(i==1)
             
{
                 
continue;
             }


             
for(j=0;j<primenum;j++
             
{
                 
if(i==a[j])
                 
{
                     number
++;
                     
break;
                 }
  
                
                 
if(i%a[j]==0)
                 
{
                     
break;
                 }


                 
if(j==(primenum-1))
                 
{
                     number
++
                 }

             }

         }

        cout
<<number<<endl;
        number
=0;   
    }
 

    
return 0
}
 
int isprime(int n)
{
    
int i;

    
for(i=2;i<=pow(n,0.5);i++)
    
{
      
if(n%i==0)
          
return 0;
    }


    
return 1
}



 

原创粉丝点击