#include <stdio.h>
#include <stdlib.h>
typedef int data_t;
/* use a cycle linked list without header node */
typedef struct node_t
{
data_t data;
struct node_t *next;
} linknode_t, *linklist_t;
int main()
{
int i, n, m, k;
linklist_t p, q;
printf("total N people will join this suicide game, please input N:");
scanf("%d", &n);
printf( "people stand in a circle, assume everyone is assigned\n"
"a sequence number from 1 to %d.\n"\
"which one would you like to start the number off (1~%d):", n, n);
scanf("%d", &k);
printf("every Xth people should kill himself, please decide the X:");
scanf("%d", &m);
if (n < 1 || k > n || m < 1) {
printf("input error!\n");
return -1;
}
printf("game is starting ...\n");
/* added the first one */
p = q = (linklist_t)malloc(sizeof(linknode_t));
p->data = 1;
/* added other left people */
for (i = 2; i <= n; i++) {
q->next = (linklist_t)malloc(sizeof(linknode_t));
q = q->next;
q->data = i;
}
/* complete the circle */
q->next = p;
/* find the people ahead of #k */
q = p;
while (q->next != p) {
if (q->next->data == k)
break;
q = q->next;
}
while (q->next != q) { /* till the last one */
/* every m people */
for (i = 0; i < m - 1; i++)
{
q = q->next;
}
/* kill the m-th people */
p = q->next;
q->next = p->next;
printf("%-2d was killed\n", p->data);
free(p);
}
/* kill the last one */
printf("%-2d was alive\n", q->data);
free(q);
return 0;
}