一个领导者选举算法

根据Matrix67的"囚徒与灯泡"文章而写:
public class Wakeup {
	static class People {
		//记录第一次进入时确定的位置 0未确定,-1 左边 1右边.
		public int Position;
		//手上的球
		public int BallNum;
		//标识
		public int Id;
		//是否还在参与跷跷板游戏
		public boolean OnLine;

		public People(int Id) {
			this.Id = Id;
			Position = 0;
			BallNum = 2;
			OnLine = true;
		}

	}

	private static int PeopleCount=100;//参与者人数
	
	private static boolean BoxIsEmpty = true;// 盒子是否是空的
	private static boolean LeftIsWeight = false;//跷跷板左边低

	public static void main(String[] args) {

		People[] thePeoples = new People[100];
		for (int i = 0; i < PeopleCount; i++) {
			thePeoples[i] = new People(i);
		}
		boolean finished = false;
		int k = 0;
		int i = 0;
		int outNum = 0;
		
		while (!finished) {
			k++;
			i = (new Random().nextInt(PeopleCount)) % PeopleCount;
			People theP = thePeoples[i];
			if (theP.OnLine) {
				if (theP.Position == 0) {
					theP.Position = LeftIsWeight ? -1 : 1;
					LeftIsWeight = !LeftIsWeight;
				} else {

					if (theP.Position == -1) {
						if (LeftIsWeight) { // 左边本侧低,如果有球则取
							if (!BoxIsEmpty) {
								BoxIsEmpty = true;
								theP.BallNum++;

							}

						} else {// 本侧高,空就放球
							if (BoxIsEmpty) {
								if (theP.BallNum > 0) {
									theP.BallNum--;
									BoxIsEmpty = false;

								}

							}
						}

					} else {

						if (LeftIsWeight) {// 本侧高,空就放球
							if (BoxIsEmpty) {
								if (theP.BallNum > 0) {
									theP.BallNum--;
									BoxIsEmpty = false;

								}

							}

						} else {// 本侧低,非空就取球
							if (!BoxIsEmpty) {
								BoxIsEmpty = true;
								theP.BallNum++;
							}

						}

					}

				}
				//如果没有球,则退出,什么都不做
				if (theP.BallNum <= 0) {
					LeftIsWeight = !LeftIsWeight;
					theP.OnLine = false;
					outNum++;
					System.out.println("Out:" + theP.Id);
				}
				//如果收集到200个球或者199个球,另外一个球在盒子里,则表示获得胜利.
				if (theP.BallNum + (BoxIsEmpty ? 0 : 1) >= PeopleCount * 2) {
					System.out.println("胜利者:" + theP.Id + ",共执行:" + k);
					finished = true;
				}
			}

		}
	}

}

 

PS:这个算法提供了一种很好的思路,但在实际选举策略中一般都不会用,因为条件不会如文章所假设的那种苛刻的场景。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值