题目:http://noi.openjudge.cn/ch0302/1748
共三种解法,可参考博客:约瑟夫环三种做法
代码:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node * next;
};
int main(void) {
int n, m;
int i;
int answer[100];
int count = 0;
struct node *head, *tail, *p, *q;
head = (struct node *) malloc(sizeof(struct node));
head->next = NULL;
while (1) {
scanf("%d,%d", &n, &m);
if (n == 0 || m == 0) {
free(head);
break;
} else {
//尾插法,生成循环链表
tail = head;
for (i = 0; i < n; ++i) {
p = (struct node *) malloc(sizeof(struct node));
p->data = i + 1;
tail->next = p;
p->next = he