#include "stdio.h"
#include "stdlib.h"
typedef struct Node {
int data;
struct Node* next;
} Node;
struct Node* create_() {
Node* G = (Node*)malloc(sizeof(Node*));
G->next = NULL;
return G;
};
void add(struct Node* G) {
Node* p = G;
Node* q;
int n;
puts("请输入旅客人数:");
scanf("%d", &n);
int i;
for (i = 1; i <= n; i++) {
q = (Node*)malloc(sizeof(Node));
q->data = i;
q->next = NULL;
p->next = q;
p = p->next;
}
p->next = G->next;
int m, k;
puts("请输入离开旅客的间隔数:");
scanf("%d", &m);
k = n / 2;
int count = 0;
p = G->next;
q = G;
puts("离开旅客的序号:");
while (p) {
count++;
if (count == m) {
count = 0;
printf("%d ", p->data);
if (p == G->next)
G = G->next;
q->next = p->next;
n--;
p = p->next;
} else {
p = p->next;
q = q->next;
}
if (n == k)
break;
}
puts("\n剩余旅客的序号:");
p = G->next;
while (p) {
printf("%d ", p->data);
p = p->next;
if (p == G->next)
break;
}
}
int main() {
Node* G = create_();
add(G);
return 0;
}
【无标题】4.约瑟夫生死问题
最新推荐文章于 2025-05-27 22:08:50 发布