当前位置: 代码迷 >> C语言 >> [求助]用筛选法求100之内的素数
  详细解决方案

[求助]用筛选法求100之内的素数

热度:126   发布时间:2006-11-10 10:59:40.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);
}
我的一种求素数的做法
----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------
  相关解决方案