每天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
原因:内存使用超出限制
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;
}
- 每天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格式)
- 全排列问题之递归求解
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
立邦乳胶漆价格表
立邦漆大桶价格表
立邦漆色卡对照表
立邦漆怎么样
立邦漆好不好
立邦漆价格表五合一
立邦漆怎么辨别真假
立邦漆价格表图片
立邦乳胶漆价格
立邦净味120价格表
立邦漆真假辨别
立邦油漆价格表
立邦漆价格表净味120
立邦漆绮得丽价格表
立邦净味120二合一
立邦内墙乳胶漆
立邦刷新服务多少钱
立邦乳胶漆厂家
立邦油漆怎么样
立邦刷新多少钱1平米
立邦刷新 价格
立邦生产厂家
立邦内墙涂料价格
立邦美得丽内墙乳胶漆价格
立邦抗甲醛五合一怎么样
立邦水性漆怎么样
立邦美得丽乳胶漆怎么样
立邦金装五合一怎么样
立邦美得丽怎么样
立邦刷新服务电话
立邦外墙漆价格表
立邦水性漆价格
立邦涂料公司怎么样
立邦内墙腻子粉价格
立邦1687怎么样
立邦油漆多少钱一桶
立邦漆代理商
立邦抗甲醛净味五合一价格
立邦工程涂料多少钱一桶
立邦工程漆价格
立邦腻子粉质量怎么样