用循环列表求解约瑟夫环,并写出完整代码
时间: 2023-03-14 11:23:51 浏览: 94
用循环列表求解约瑟夫环的完整代码如下:n = int(input("请输入总人数:"))
m = int(input("请输入报数:"))people = list(range(1, n + 1))i = 0
k = 0while people[i] != 0:
if k == m-1:
print("编号为%d的人出局" %people[i])
people[i] = 0
k = 0
i = -1
i = i + 1
if i == n:
i = 0
k = k + 1
相关问题
c语言循环链表实现约瑟夫环完整代码
当然可以!下面是C语言实现约瑟夫环的完整代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义循环链表的节点结构
typedef struct Node {
int data;
struct Node* next;
} Node;
// 创建循环链表
Node* createCircularLinkedList(int n) {
Node* head = NULL;
Node* prev = NULL;
for (int i = 1; i <= n; i++) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = i;
if (head == NULL) {
head = newNode;
} else {
prev->next = newNode;
}
newNode->next = head;
prev = newNode;
}
return head;
}
// 删除指定位置的节点
Node* deleteNode(Node* head, int position) {
if (head == NULL) {
return NULL;
}
Node* current = head;
Node* prev = NULL;
// 找到要删除的节点
for (int i = 1; i < position; i++) {
prev = current;
current = current->next;
}
// 删除节点
if (prev == NULL) {
prev = current;
while (prev->next != head) {
prev = prev->next;
}
prev->next = current->next;
head = current->next;
} else {
prev->next = current->next;
}
free(current);
return head;
}
// 约瑟夫环算法
void josephusCircle(Node* head, int m, int n) {
Node* current = head;
// 找到起始位置
for (int i = 1; i < m; i++) {
current = current->next;
}
// 开始约瑟夫环
while (n > 0) {
printf("%d ", current->data);
head = deleteNode(head, m);
current = current->next;
n--;
}
}
int main() {
int n, m;
printf("请输入总人数n:");
scanf("%d", &n);
printf("请输入报数m:");
scanf("%d", &m);
Node* head = createCircularLinkedList(n);
printf("约瑟夫环的顺序为:");
josephusCircle(head, m, n);
return 0;
}
```
希望对你有帮助!如果还有其他问题,请随时提问。
java实现双链表求解约瑟夫环问题,并输出出环顺序
下面是Java实现双链表求解约瑟夫环问题的代码,其中包括了输出环顺序的部分:
```
import java.util.Scanner;
public class Josephus {
private Node head;
private Node tail;
private int size;
private class Node {
private int data;
private Node prev;
private Node next;
public Node(int data) {
this.data = data;
}
}
public Josephus() {
head = null;
tail = null;
size = 0;
}
public void add(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = head;
} else {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
}
size++;
}
public void remove(Node node) {
if (node == head) {
head = node.next;
} else if (node == tail) {
tail = node.prev;
} else {
node.prev.next = node.next;
node.next.prev = node.prev;
}
size--;
}
public void solve(int m) {
Node p = head;
while (size > 1) {
for (int i = 0; i < m - 1; i++) {
p = p.next;
}
Node temp = p.next;
remove(p);
p = temp;
}
System.out.println("The order of the Josephus circle is:");
System.out.print(head.data + " ");
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the number of people:");
int n = scanner.nextInt();
System.out.println("Please enter the value of m:");
int m = scanner.nextInt();
Josephus josephus = new Josephus();
for (int i = 1; i <= n; i++) {
josephus.add(i);
}
josephus.solve(m);
scanner.close();
}
}
```
在这个程序中,我们首先定义了一个双链表类`Josephus`,其中包括了`Node`节点类和相应的链表操作,比如`add`、`remove`等。
在`solve`方法中,我们使用一个指针`p`来遍历链表,每次移动`m-1`个位置,然后将`p`指向的节点删除。直到链表中只剩下一个节点,输出该节点的值。
最后,在`main`方法中,我们输入人数`n`和`m`,然后创建一个`Josephus`对象,并将人数添加到双链表中。最后调用`solve`方法求解约瑟夫环问题,并输出环顺序。