之前用的是循环链表,java刚学,不知道怎么用链表,用个小算法吧
代码:
import java.util.Scanner; /** * */ /** * @author john * @约瑟夫循环/MonkeyKing */ public class MonkeyKing { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.print("请输入m,n:"); Scanner scanner = new Scanner (System.in); int m =scanner.nextInt() ; int n =scanner.nextInt() ; System.out.print("第"+findKing(m,n)+"只猴子为猴王"); } public static int findKing(int m, int n) { int MonkyKingIndex = 0; for (int i = 2; i <= m; ++i) { MonkyKingIndex = (MonkyKingIndex + n) % i; } return MonkyKingIndex+1; } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/hhooong/article/details/46874979