约瑟夫的幸存者问题


问题描述:

         n个人编号为1~n,围成一个圈,从第一个人开始报数,123123。。。,报到3的人被踢出,如此循环,直到最后剩下一个人,请问这个人的编号是多少??


方法一:数组解法


import java.util.Scanner;


public class Joseph {

	int[] arr;
	Joseph(int e) {
		arr = new int[e];
	}
	public static void main(String[] args) {
		System.out.print("请输入人数: ");
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		Joseph j = new Joseph(num);
		for(int i=1; i<=num; i++) {
			j.arr[i-1] = i;
		}
		j.cal();

	}
	
	void cal() {
		int count = 0;
		int i = 0;
		int sum = 0;
		while(count != arr.length - 1) {
			if(arr[i] != 0) {
				sum++;
				if(sum == 3) {
					count ++;
					arr[i] = 0;
					sum = 0;
				}
			}
			if(i == arr.length - 1) {
				i = 0;
			} else {
				i++;
			}
		}
		
		
		for(i=0; i<arr.length; i++) {
			if(arr[i] != 0) {
				System.out.println("幸存者是: " + arr[i] + "号");
			}
		}
	}

}


方法二:循环单链表解法


import java.util.Scanner;


public class Joseph {

	Node head;
	Joseph(int e) {
		head = new Node();
		Node p = head;
		for(int i=1; i<=e; i++) {
			Node n = new Node(i);
			p.next = n;
			p = n;
		}
		p.next = head.next;
		head = head.next;
	}
	
	void display() {
		Node p = head;
		while(p.next != head) {
			System.out.print(p.data + "->");
			p = p.next;
		}
		System.out.println(p.data);
	}
	public static void main(String[] args) {
		System.out.print("请输入人数: ");
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		Joseph j = new Joseph(num);
	
		j.display();
		j.cal();

	}
	
	void cal() {
		
		Node p = head;
		int sum = 0;
		while(p.next != p) {
	
			sum++;
			if(sum == 3) {
				
				Node pre = pre(p);
				System.out.println("删除 " + p.data + "号");
				pre.next = pre.next.next;
				
				sum = 0;
			} 
			p = p.next;

		}

		System.out.println("幸存者是: " + p.data + "号");	
	}
	
	Node pre(Node p) {
		Node pre = p;;
		while(pre.next != p) {
			pre = pre.next;
		}
		
		return pre;
	}

}

class Node {
	int data;
	Node next;
	Node(){}
	Node(int e) {
		data = e;
	}
}

结果:

请输入人数: 9
1->2->3->4->5->6->7->8->9
删除 3号
删除 6号
删除 9号
删除 4号
删除 8号
删除 5号
删除 2号
删除 7号
幸存者是: 1号


方法三:队列解法


import java.util.LinkedList;
import java.util.Scanner;


public class Joseph {

	LinkedList<Integer> list = new LinkedList<Integer>();
	Joseph(int e) {
		for(int i=1; i<=e; i++) {
			list.add(i);
		}
	}
	public static void main(String[] args) {
		System.out.print("请输入人数: ");
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		Joseph j = new Joseph(num);
		j.cal();
	}
	
	void cal() {
		
		
		int sum = 0;
		while(list.size() != 1) {
	
			sum++;
			if(sum == 3) {
				int e = list.removeFirst();
				System.out.println("删除 " + e + "号");
				sum = 0;
			} else {
				int e = list.removeFirst();
				list.add(e);
			}



		}

		System.out.println("幸存者是: " + list.get(0) + "号");
		
	}

}


or


import java.util.Scanner;


public class Joseph {

	LinkedQueue lq = new LinkedQueue();
	Joseph(int e) {
		for(int i=1; i<=e; i++) {
			lq.enQueue(i);
		}
	}
	
	
	public static void main(String[] args) {
		System.out.print("请输入人数: ");
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		Joseph j = new Joseph(num);
		j.lq.display();
		j.cal();

	}
	
	void cal() {
		
		int sum = 0;
		while(lq.length() != 1) {
	
			sum++;
			if(sum == 3) {
				int[] e = lq.deQueue();
				System.out.println("删除 " + e[1] + "号");
				sum = 0;
			} else {
				int[] e = lq.deQueue();
				lq.enQueue(e[1]);
			}

		}

		System.out.println("幸存者是: " + lq.qh.front.data + "号");
	}
	
}

class LinkedQueue {

	QueueHead qh = new QueueHead();
	
	boolean empty() {
		return qh.front == null;        //or qh.rear == null
	}
	
	void enQueue(int e) {
		QNode q = new QNode(e);
		if(empty()) {
			qh.front = q;
			qh.rear = q;
		} else {
			qh.rear.next = q;
			qh.rear = q;
		}
	}
	
	int[] deQueue() {
		int[] arr = new int[2];
		if(qh.rear == null) {
			arr[0] = 0;
			return arr;
		}
		arr[0] = 1;
		arr[1] = qh.front.data;
		if(qh.front.next == null) {
			qh.front = qh.rear = null;
		} else {
			qh.front = qh.front.next;
		}
		return arr;
	}
	
	int length() {
		int sum = 0;
		QNode q = qh.front;
		while(q != null) {
			sum++;
			q = q.next;
		}
		return sum;
	}
	
	void display() {
		QNode p = qh.front;
		while(p != null) {
			if(p.next == null) {
				System.out.println(p.data);
			} else {
				System.out.print(p.data + "->");
			}
			p = p.next;
		}
	}

}

class QNode {
	int data;
	QNode next;
	QNode(){}
	QNode(int e) {
		data = e;
	}
}

class QueueHead {
	QNode front;
	QNode rear;
	public QueueHead() {}
}


结果:

请输入人数: 8
删除 3号
删除 6号
删除 1号
删除 5号
删除 2号
删除 8号
删除 4号
幸存者是: 7号




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值