We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ae3fa46 commit 3c645f3Copy full SHA for 3c645f3
archive/g/go/josephus-problem.go
@@ -0,0 +1,37 @@
1
+package main
2
+
3
+import (
4
+ "os"
5
+ "strconv"
6
+)
7
8
+func die() {
9
+ println("Usage: please input the total number of people and number of people to skip.")
10
+ os.Exit(1)
11
+}
12
13
+func josephus(n, k int) int {
14
+ if n == 1 {
15
+ return 1
16
+ }
17
18
+ return (josephus(n-1, k)+k-1)%n + 1
19
20
21
+func main() {
22
+ if len(os.Args) != 3 {
23
+ die()
24
25
26
+ n, err := strconv.Atoi(os.Args[1])
27
+ if err != nil {
28
29
30
31
+ k, err := strconv.Atoi(os.Args[2])
32
33
34
35
36
+ println(josephus(n, k))
37
0 commit comments