标题:[求助]用筛选法求100之内的素数
只看楼主
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
得分:0 
恩,我又出错了...

倚天照海花无数,流水高山心自知。
2006-11-10 10:59
半滴风雨
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2006-10-6
得分:0 
#include<stdio.h>
#include<math.h>
struct node
{
int a;
struct node *p;
}typedef Node;
Node *Create()
{
Node *Pb;
Pb = new Node;
Pb->p = NULL;
return Pb;
}
Node *Insert(Node *Pa,int n)
{
Node *Pb;
Pb = new Node;
Pb->a = n;
Pa->p = Pb;
Pb->p = NULL;
return Pb;
}
void Output(Node *Pa)
{
Pa = Pa->p;
while(Pa)
{
printf("%d ",Pa->a);
Pa = Pa->p;
}
printf("\n");
}
void Destroy(Node *Pa)
{
Node *Pb;
while(Pa)
{
Pb = Pa->p;
delete Pa;
Pa = Pb;
}
}
void main()
{
int Ma,i,Ni=1;
Node *qa,*head,*qc;
head = Create();
qa = Insert(head,2);
qc = qa;
printf("Please input the number!\n");
scanf("%d",&Ma);
for(i=3;i<=Ma;i=i+2)
{ Node *qb;
qb = qc;
while(qb)
{
if(i%qb->a==0)
break;
qb = qb->p;
}
if(!qb)
{ qa = Insert(qa,i);
Ni++;
}
}
Output(head);
printf("%d\n",Ni);
Destroy(head);
}
我的一种求素数的做法
2006-11-10 12:51
水漾花
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2006-11-9
得分:0 

谢谢各位亲朋好友的鼎立相助,略施薄礼,不成敬意,还请各位笑纳!


2006-11-11 20:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-101840-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.434714 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved