- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathjoshephus.java
37 lines (36 loc) · 783 Bytes
/
joshephus.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
importjava.util.*;
classjoshephus
{
staticintJos(intn, intk)
{
if(n == 1)
return1;
return (Jos(n - 1,k) + k-1) % n + 1;
}
publicstaticvoidmain(Stringargs[])
{
ScannerI = newScanner(System.in);
intn = I.nextInt(), k = I.nextInt();
System.out.println(Jos(n,k));
I.close();
}
}
/*when starting pos is 0
import java.util.*;
class joshephus
{
static int Jos(int n, int k)
{
if(n == 1)
return 0;
return (Jos(n - 1,k) + k) % n;
}
public static void main(String args[])
{
Scanner I = new Scanner(System.in);
int n = I.nextInt(), k = I.nextInt();
System.out.println(Jos(n,k));
I.close();
}
}
*/