每天OnlineJudge之 “数素数”
来源:互联网 发布:node 单进程 编辑:程序博客网 时间:2024/05/18 04:01
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
原因:内存使用超出限制
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;
}
此解答未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=0; int 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;
}
#include<cmath>
using namespace std;
int isprime(int);
int main()
{
int m,n;int i,j;int number=0; int 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;
}
文章来源于 http://www.cnblogs.com/zhouyinhui 版权归原作者所有
- 每天OnlineJudge之 “数素数”
- 每天OnlineJudge之 “数素数”
- 每天OnLineJudge 之 “蛇形矩阵 ”
- 每天OnLineJudge 之 “蛇形矩阵 ”
- 每天OnLineJudge 之 “杨辉三角 ”
- 每天OnLineJudge 之 “杨辉三角 ”
- 数七之素数
- onlineJudge
- 九度OnlineJudge题目1064:反序数
- 初试牛客网PAT练习之数素数
- python3之1013.数素数 (19分)
- 九度OnlineJudge之1468:Sharing
- 九度OnlineJudge之1002:Grading
- 九度OnlineJudge之1003:A+B
- 九度OnlineJudge之1004:Median
- 九度OnlineJudge之1010 A + B
- 九度OnlineJudge之1012:畅通工程
- 九度OnlineJudge之1014:排名
- 实现页面的分帧显示
- 开源: 个人财务管理系统文档与源码下载
- TongJI Online Judge预赛(2): LOVE LETTER
- 表达式计算算法总结
- 同济OnlineJudge预赛题(一): Treenders
- 每天OnlineJudge之 “数素数”
- 堆栈小应用:配对
- 每天OnLineJudge 之 “蛇形矩阵 ”
- 每天OnLineJudge 之 “杨辉三角 ”
- 小问题,对递归重复调用的改进,一起来分享
- 在.net中使用Udp协议创建简单的聊天程序
- 以前我自己设计的“俄罗斯方块”,觉得挺有意思,今天贴出来
- 好书推荐 : 《深入理解计算机系统》(中文版,pdf格式)
- 全排列问题之递归求解