拉丁方阵问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
int data;
struct Node *next;
} Node;

Node *createLinkedList(int length)
{
Node *last, *this, *head;
head = malloc(sizeof(Node));
head->data = 0;
head->next = NULL;
last = head;
for (int i = 1; i < length; i++)
{
this = malloc(sizeof(Node));
this->next = NULL;
this->data = i;
last->next = this;
last = this;
}
last->next = head;
return head;
}

void solve(Node *head, int n)
{
Node *p = head;
for (int i = 0; i < n; i++)
{
for (int a = 0; a < n; a++)
{
printf("%d ", p->data);
p = p->next;
}
p = p->next;
printf("\n");
}
}

int main(int argc, char const *argv[])
{
Node *linkedlist = NULL;
int n = 0;
scanf("%d", &n); //输入n
linkedlist = createLinkedList(n);
solve(linkedlist, n);
return 0;
}

查找链表中间元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
int data;
struct Node *next;
} Node;

Node *createLinkedList(int length)
{
Node *last, *this, *head;
head = malloc(sizeof(Node));
head->data = 0;
head->next = NULL;
last = head;
for (int i = 1; i < length; i++)
{
this = malloc(sizeof(Node));
this->next = NULL;
this->data = i;
last->next = this;
last = this;
}
last->next = NULL;
return head;
}

int solve(Node *head)
{
//快的指针移动速度是慢的的两倍,当快指针移动到末尾则慢指针刚好到达中间
Node *slow = head ,*fast = head;
while (1)
{
if (fast == NULL)
{
return slow->data;
}
slow = slow->next;
fast = fast->next;
if (fast != NULL){
fast = fast->next;
}
}
}

int main(int argc, char const *argv[])
{
Node *linkedlist = NULL;
int n = 0;
scanf("%d", &n);
linkedlist = createLinkedList(n);
printf("%d",solve(linkedlist));
return 0;
}

约瑟夫环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
int data;
struct Node *next;
} Node;

Node *createLinkedList(int length)
{
Node *last, *this, *head;
head = malloc(sizeof(Node));
head->data = 0;
head->next = NULL;
last = head;
for (int i = 1; i < length; i++)
{
this = malloc(sizeof(Node));
this->next = NULL;
this->data = 0;
last->next = this;
last = this;
}
last->next = head;
return head;
}

void solve(Node *head, int n,int skip)
{
Node *p = head;
int count = 1;
for (int i = 0; i < n; i++)
{
for (int a = 0; a < skip; a++)
{
p = p->next;
if (p->data != 0) //如果这里的人已经死了那么就必须重新找下一个
{
a--;
}
}
p->data = count;
count++;
}
}

void show(Node *head){
Node *p = head;
while (1)
{
printf("%d ",p->data);
p = p->next;
if (p == head)
{
return;
}

}
}

int main(int argc, char const *argv[])
{
Node *linkedlist = NULL;
int n = 0;
scanf("%d", &n);
linkedlist = createLinkedList(n);
solve(linkedlist, n,3);
show(linkedlist);
return 0;
}