大家好,又見面了,我是全棧君。
之前用的是循環連結清單,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;
}
}
複制
釋出者:全棧程式員棧長,轉載請注明出處:https://javaforall.cn/115587.html原文連結:https://javaforall.cn