其他分享
首页 > 其他分享> > SWUST OJ(956)

SWUST OJ(956)

作者:互联网

约瑟夫问题的实现

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 typedef struct LNode
 5 {
 6     int data;
 7     struct LNode *next;
 8 }LinkList;
 9 
10 void CreateList(LinkList *& L, int l)
11 {
12     L =(LinkList*)malloc(sizeof(LinkList));
13     LinkList *r;
14     r = L;
15     r->next = L;
16     LinkList * p;
17 
18     for (int i = 0; i < l; ++i)
19     {
20         p =(LinkList*)malloc(sizeof(LinkList));
21         p->data = i+1;
22         p->next = L;
23         r->next = p;
24         r = p;
25     }
26 }
27 
28 LinkList* find(LinkList *p, int n, int &e, LinkList *L)
29 {
30     LinkList * r;
31     r = p;
32     for(int i = 0;i<n-1; i++)
33     {
34         r = r->next;
35         if (r == L)
36         {
37             r = r->next;
38         }
39     }
40     LinkList * q;
41     if (r->next == L)
42     {
43         q = r->next->next;
44     }
45     else
46     {
47         q = r->next;
48     }
49     e = q->data;
50     r->next = q->next;
51     return q;
52 }
53 
54 int main(int argc, char const *argv[])
55 {
56     LinkList *L;
57     int n,k;
58     scanf("%d",&n);
59     scanf("%d",&k);
60     CreateList(L,n);
61     int e;
62     LinkList* p;
63     p = L;
64     for (int i = 0; i < n; ++i)
65     {
66         p = find(p,k,e,L);
67     }
68     printf("%d",e);
69     return 0;
70 }

 

标签:OJ,int,struct,next,LinkList,SWUST,data,find,956
来源: https://www.cnblogs.com/Ghost4C-QH/p/10536930.html