#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);
}
我的一种求素数的做法