每天OnlineJudge之 “数素数”

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

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

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

None.gif
None.gif#include
<stdio.h>
None.gif#include
<math.h>
None.gif#include
<malloc.h>
None.gif
None.gif
int* arrIs;
None.gif
int count=0;
None.gif
int start=0;
None.gif
int end=0;
None.gif
None.gif
int Is(int n)
ExpandedBlockStart.gif
{
InBlock.gif    
if(arrIs[n] != -1)
ExpandedSubBlockStart.gif    
{
InBlock.gif        
return arrIs[n];
ExpandedSubBlockEnd.gif    }

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

InBlock.gif        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    arrIs[n]
=1;
InBlock.gif    
return 1;
ExpandedBlockEnd.gif}

None.gif
None.gif
int main()
ExpandedBlockStart.gif
{
InBlock.gif
InBlock.gif    
while(scanf("%d",&start)!=EOF && scanf("%d",&end)!=EOF)
ExpandedSubBlockStart.gif    
{
InBlock.gif        count 
=0;
InBlock.gif
InBlock.gif        arrIs 
= (int*)malloc((end+1)*4);
InBlock.gif
InBlock.gif        
for(int i=0; i<=end; i++)
ExpandedSubBlockStart.gif        
{
InBlock.gif            arrIs[i] 
= -1;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
for(int j=start; j<=end; j++)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
if(Is(j))
InBlock.gif                count
++;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        printf(
"%d",count);
InBlock.gif        
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
None.gif
None.gif
None.gif

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


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

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

None.gif
None.gif
None.gif#include
<stdio.h>
None.gif#include
<math.h>
None.gif#include
<malloc.h>
None.gif
None.gif
None.giftypedef 
struct node_
ExpandedBlockStart.gif
{
InBlock.gif    
int num;
InBlock.gif    node_ 
* next;
ExpandedBlockEnd.gif}
node;
None.gif
None.gif
None.gifnode
* nodesHead;
None.gif
int count=0;
None.gif
int start=0;
None.gif
int end=0;
None.gif
None.gif
None.gif
int Is(int);
None.gif
int Find(int);
None.gif
None.gif
None.gif
None.gif
int Is(int n)
ExpandedBlockStart.gif
{
InBlock.gif    
if(Find(n))
ExpandedSubBlockStart.gif    
{
InBlock.gif        
return 1;
ExpandedSubBlockEnd.gif    }

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

InBlock.gif        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }
else if(n<=1)
ExpandedSubBlockStart.gif    
{
InBlock.gif        
return 0;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    node 
* nd = (node*)malloc(sizeof(node));
InBlock.gif    nd
->num = n;
InBlock.gif    nd
->next = nodesHead->next;
InBlock.gif    nodesHead
->next = nd;
InBlock.gif
InBlock.gif    
return 1;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
int Find(int n)
ExpandedBlockStart.gif
{
InBlock.gif    node
* h = nodesHead;
InBlock.gif    
while(h->next)
ExpandedSubBlockStart.gif    
{
InBlock.gif        
if(h->num == n)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
return 1;
ExpandedSubBlockEnd.gif        }

InBlock.gif        h
=h->next;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
int main()
ExpandedBlockStart.gif
{
InBlock.gif    nodesHead 
= (node*)malloc(sizeof(node));
InBlock.gif    nodesHead
->num=2;
InBlock.gif    nodesHead
->next=0;
InBlock.gif    
while(scanf("%d",&start)!=EOF && scanf("%d",&end)!=EOF)
ExpandedSubBlockStart.gif    
{
InBlock.gif        count 
=0;
InBlock.gif
InBlock.gif        
for(int j=start; j<=end; j++)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
if(Is(j))
InBlock.gif                count
++;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        printf(
"%d/n",count);
InBlock.gif        
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}


最终的解答,正确的

None.gif#include<iostream> 
None.gif#include
<cmath> 
None.gif
using namespace std;
None.gif
int isprime(int); 
None.gif
None.gif
int main()
ExpandedBlockStart.gif
{
InBlock.gif    
int m,n;int i,j;int number=0int primenum=0
InBlock.gif    
int a[170]; 
InBlock.gif    
for(i=2;i<=1000;i++)
ExpandedSubBlockStart.gif    

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

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

InBlock.gif
InBlock.gif                 
if(j==(primenum-1))
ExpandedSubBlockStart.gif                 
{
InBlock.gif                     number
++
ExpandedSubBlockEnd.gif                 }

ExpandedSubBlockEnd.gif             }

ExpandedSubBlockEnd.gif         }

InBlock.gif        cout
<<number<<endl;
InBlock.gif        number
=0;   
ExpandedSubBlockEnd.gif    }
 
InBlock.gif
InBlock.gif    
return 0
ExpandedBlockEnd.gif}
 
None.gif
int isprime(int n)
ExpandedBlockStart.gif
{
InBlock.gif    
int i;
InBlock.gif
InBlock.gif    
for(i=2;i<=pow(n,0.5);i++)
ExpandedSubBlockStart.gif    
{
InBlock.gif      
if(n%i==0)
InBlock.gif          
return 0;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
return 1
ExpandedBlockEnd.gif}

None.gif
None.gif


 


文章来源于 http://www.cnblogs.com/zhouyinhui 版权归原作者所有
阅读更多
文章标签: output input 测试
个人分类: .NET
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭