告诉我java代码
时间: 2023-07-10 16:21:10 浏览: 32
以下是Java代码实现:
```
public class JosephProblem {
public static int joseph(int n) {
if (n == 1) {
return 0;
} else {
return (joseph(n - 1) + 2) % n;
}
}
public static int joseph(int n, int k) {
int s = 0;
for (int i = 2; i <= n; i++) {
s = (s + k) % i;
}
return s + 1;
}
public static int joseph(int n, int m, int k) {
int p = 1;
int t = 0;
for (int i = 2; i <= n; i++) {
p = (p * m) % i;
}
for (int i = 1; i <= n; i++) {
t = (t + k * p) % i;
}
return t + 1;
}
public static void main(String[] args) {
int n = 10;
int k = 3;
int m = 2;
System.out.println("The solution to Joseph problem with n=" + n + " is " + joseph(n));
System.out.println("The solution to Joseph problem with n=" + n + " and k=" + k + " is " + joseph(n, k));
System.out.println("The solution to Joseph problem with n=" + n + ", m=" + m + " and k=" + k + " is " + joseph(n, m, k));
}
}
```
其中,joseph(n)表示只有一组人的情况,joseph(n, k)表示k为步长的情况,joseph(n, m, k)表示m为组数的情况。