題目:求一個包含小於a的質數的鏈表.
鏈表採用動態分配方式,雖然效率不是很高,但很實用.對於求的a較小時能較好與其它接口耦合
//求小於a的質數 ,不含1//!!!!!要與結構體合用,生成一個小於a的質數鏈表//head頭w尾p過渡指針
//由於不能定義temp類型的指針,所以無法返回值,所以此代碼只能在程序塊內作組成部份,不能單獨作爲一個函數來使用
void primenumber(_int64 a)
{
struct temp
{
_int64 b;
temp* next;
};
temp *p,*head=new temp;//ini
head->b=2;//從2開始
head->next=new temp;
temp *w=head->next;//head頭w尾p過渡指針
w->next=NULL;
for(_int64 i=3;i<=a;i++)//從1到a遍除質數
{
bool j=false;//標記i是否除質數爲0
for(p=head;p->next!=NULL;p=p->next)
{
if(!(i%p->b))
{j=true;
break;
}
}
if(!j)//如果與所有質數沒除盡,表示是質數
{
w->b=i;
w->next=new temp;
w=w->next;
w->next=NULL;
}
}
}