鍊錶經典例題

2022-01-31 13:51:39 字數 2067 閱讀 5376

d.基礎資料結構——單鏈表(2)(鍊錶)

time limit: 1000 ms

memory limit: 32768 k

total submit: 192 (72 users)

total accepted: 82 (67 users)

special judge: no

description

1997-1998 年歐洲西南亞洲區預賽之後,舉辦了一場隆重的聚會。主辦方發明了乙個特殊的方式去挑選那些自願去洗髒碟子的參賽選手。先讓那些選手乙個挨著乙個的排成一條隊。每個選手都獲得乙個編號,那些編號是從2開始的,第乙個的編號是2 ,第二個人的編號是3,第三個人的編號是4,以此類推。

第乙個選手將被問到他的編號(編號為2)。他將不用去清洗了,直接參加聚會,但是他身後的所站的位置是2的倍數的人必須去廚房(那些人的編號分別為4,6,8 等等)。然後在那隊伍中的下乙個選手必須報數。他回答3,他可以離開去參加聚會,但是在他身後的每個是三的倍數的選手將會被選上(那些人的編號分別為9,15,21等等)。下乙個被選上的人的編號是5,並且將可以離開去參加聚會,但是在他身後並且站的位置是5的倍數的人將會被選上去清洗碟子(那些人的編號分別為19,35,49等等).下乙個被選上的人的編號是7,並且將可以離開去參加聚會,但是在他身後並且站的位置是7的倍數的人將會被選上去清洗碟子,以此類推。

讓我們稱那些沒有被選上去洗碟子的那些選手的編號為幸運數字。繼續這個挑選的方式,那些幸運的數字是2,3,5,7,11 ,13,17等等的遞增序列。為下一次的聚會尋找幸運數字!

input

本題有多組測試資料,每組測試資料報含乙個整數n,1<=n<=3000。

output

對於每組測試資料輸出乙個數字,代表對應的幸運號碼。

sample input

1 2 

10 20

sample output

2 3 

29 83 

#include #include 

#include

#define true 1

#define false 0

#define ok 1

#define error 0

#define infeasible -1

#define overflow -2

#define list_init_size 100

#define listincrement 10

#define status int

#define int inttypedef

struct

lnodenode,*linklist;

void init(linklist &l)

intmain()

int flag=0

; linklist top;

while(l1->next!=null)

top=top->next;

}if(flag)

else}}

l1=l1->next;

}intt;

while(scanf("

%d",&t)!=eof)

printf(

"%d\n

",ans->date);

}return0;

}/*#include#includestruct node

;int main()

p->next = null;

//建立完畢。

struct node *point = head;

struct node *top;

int ans = 0;

//遍歷鍊錶。

while(point->next != null)

top = top->next;

}if(ans)

else

else}}

point = point->next;

}int n;

while(scanf("%d", &n) != eof)

printf("%d\n", point->data);

}return 0;

}*/

單向鍊錶例題

include include struct node 定義結點的結構體 typedef struct node node 將struct node簡單定義為node typedef struct node link 將stcuct node型別定義為 link void create link l...

鍊錶例題練習

例題描述 將鍊錶中值小於x的節點放在鍊錶的前面,值大於等於x的節點放在鍊錶的後半部分。class listnode public listnode int val public class linkedlistinterview else send cur send指向cur所指的物件,當send指...

經典鍊錶問題

題目描述 在單鏈表中輸出倒數第k個節點 要求 如果鍊錶長為n,時間複雜度為o n 額外空間複雜度達到o 1 思路 當我們用num來表示鍊錶中節點個數,當我們輸出節點的時候會出現三種情況 不存在第k個節點,此時返回空 num第k個節點就是第乙個節點,操作較容易 num k 第k個節點在鍊錶中 num ...