package com.bupt.syc;import java.util.Iterator;
import java.util.LinkedList;import org.junit.Test;public class Josephus {// 模拟解法public static int JosephusWinner(int M, int N) throws Exception {if (N < M) {throw new Exception("M should smaller than N");}int arr[] = new int[N];for (int i = 0; i < N; i++) {arr[i] = 1;}int cnt = 0, k = 0, j = 0;int num=-1;while(cnt<=N-1){if(arr[k]==1){j+=arr[k];}if(j==M){arr[k]=0;cnt++;}}return num;}//公式法//f(1)=0//f=(f+m)mod i (i>1)public static int JosephusWinner2(int M ,int N){int winner=0;for(int i=2;i<=N;i++){winner=(winner+M)%i;}return winner;}public static void JosephusWinner3(int M ,int N){LinkedList <Integer> linkedList=new LinkedList<Integer>();for(int i=0;i<N;i++){linkedList.add(i);}int num=0;while(linkedList.size()>1){Iterator iterator=linkedList.iterator();while(iterator.hasNext()){num++;iterator.next();if(num==M){iterator.remove();num=0;}}}if(linkedList.size()==1){System.out.println("winner is :"+linkedList.get(0));}}//题目:有N个人围成一圈,每人有一个编号,从编号为1的人开始,每隔M个出圈,按出圈次序排成一列,其编号刚好按顺序从1到M。 //要求:从键盘输入M,N,编程计算并输出这N个人原来在圈中的位置。public static void reverseJosephusWinner(int M,int N){LinkedList <Integer> linkedList=new LinkedList<Integer>();for(int i=0;i<N;i++){linkedList.add(0);}int num=1;int cnt=0;int index=0;while(num<=N){if(linkedList.get(index)==0){cnt++;}if(cnt==M&&linkedList.get(index)==0){linkedList.set(index, num);num++;cnt=0;for(int i=0;i<N;i++){System.out.print(linkedList.get(i)+" ");}System.out.println();}if(index+1>N-1) index=0;else index++;}}public static void main(String args[]) {JosephusWinner3(5,9);reverseJosephusWinner(5,9);}
}

转载于:https://www.cnblogs.com/waka401/archive/2012/08/12/2635249.html

Josephus 问题相关相关推荐

  1. 数据结构源码笔记(C语言):Josephus问题之顺序表

    /*josephus_seq.c*/ /*Josephus问题: 顺序表实现*/#include <stdio.h> #include <stdlib.h>#define FA ...

  2. 数据结构源码笔记(C语言):Josephus问题之循环链接表

    /*josephus_clist.c*/ /*Josephus问题:循环链接表实现*/#include <stdio.h> #include <stdlib.h>#define ...

  3. 数据结构源码笔记(C语言):哈希表的相关运算算法

    //实现哈希表的相关运算算法 #include<stdio.h> #include<malloc.h> #include<string.h>#define MaxS ...

  4. 数据结构源码笔记(C语言):二叉平衡树的相关操作算法

    //二叉平衡树的相关运算 #include<stdio.h> #include<malloc.h> #include<string.h>typedef char I ...

  5. 数据结构源码笔记(C语言):B树的相关运算算法

    //B树的相关运算算法#include<stdio.h> #include<malloc.h>#define MAXM 10//定义B树最大的阶数 typedef int Ke ...

  6. 转贴:Josephus问题

    原文地址及原作者不详 1. 问题的由来 Josephus问题是以10世纪的著名历史学家Flavius Josephus命名的. 据说, Josephus如果没有数学才能, 他就不会在活着的时候出名! ...

  7. Redis 笔记(02)— keys 键相关命令(查询数据库key数量、判断key是否存在、指定key过期时间、查看key类型、查看key剩余秒数、选择数据库、删除key、删除数据库)

    1. keys 键相关命令 命令 说明 dbsize 返回当前数据路的key数量 exists key 测试指定key是否存在 expire key seconds 为key指定过期时间 type k ...

  8. 国民技术芯片相关产业研发

    国民技术芯片相关产业研发 国民技术股份有限公司--塑造网络社会信息安全DNA,以原始创新技术满足人们安全便捷的网络生活需求.2000年公司成立,是承担国家"909"超大规模集成电路 ...

  9. C++ OP相关注意事项

    C++ OP相关注意事项 Paddle中Op的构建逻辑 1.Paddle中Op的构建逻辑 Paddle中所有的Op都继承自OperatorBase,且所有的Op都是无状态的,每个Op包含的成员变量只有 ...

最新文章

  1. 原生javascript实现放大镜效果
  2. Windows IIS7 下安装配置 PHP7.0
  3. UGUI实现接口事件
  4. Libra教程之:来了,你最爱的Move语言
  5. 【303】C# 复制窗体 修改名称
  6. mysql结果集键值对_键值对集合DictionaryK,V根据索引提取数据
  7. 云信api_服务端API文档
  8. 超实用windows软件推荐!新电脑必装哪些软件?
  9. android 开机动画xp,XP下更改开机动画
  10. skype 无法连接
  11. grep匹配单引号('),惰性匹配(.*?)
  12. 小胖儿 闲聊 百度有啊
  13. 5、html标签中的两种关系
  14. 拉新、复购、供应链,双11中小商家“三难”如何破?
  15. CRC-CCITT16(0xFFFF、XModem、0x1D0F、Kermit)
  16. 关联规则—频繁项集Apriori算法
  17. Mysql 数据库实战-建库-建表-查多表
  18. 上海地铁+android+nfc,在上海手机竟然能当交通卡用!最全攻略来了!
  19. java写入excel模板_java实现赋值excel模板,并在新文件中写入数据,并且下载
  20. PHPExcel用法总结

热门文章

  1. JZOJ 3807. 【NOIP2014模拟8.25】地砖铺设
  2. java cdi是什么意思_Java EE CDI的真正好处
  3. python怎么把cpu占满_如何增加python CPU使用率
  4. echarts柱状图显示百分比_Echarts 饼状图显示信息,内容,值,百分比都显示的代码 更改图例等问题汇总...
  5. 蚁群算法,PSO算法以及两种算法可以融合的几种方法
  6. 百度超级搜索技巧集锦
  7. java 有序set_Java 从Set里面取出有序的记录详解及实例
  8. system函数阻塞_CompletableFuture:让你的代码免受阻塞之苦
  9. python websocket server_Python Websocket服务端
  10. 20211201 二范数的导数小于等于导数的二范数(导数存在情况下)