1 public class josephus { private static class node { int val; node next; } public static void...

63
1 public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; } Josephus Problem: Build the Circular Linked List 0 null head

Upload: barnaby-beasley

Post on 22-Dec-2015

480 views

Category:

Documents


4 download

TRANSCRIPT

1

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

0

null

head

2

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

null

head

3

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

4

Josephus Problem: Build the Circular Linked List

1

head

tail

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

5

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

6

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

0null

x

7

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2null

x

8

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

9

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

10

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

11

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

12

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

0

null

13

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

3

null

14

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

3

15

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

3

16

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

3

17

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2

x

3

18

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

23

0

null

x

19

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

23

4

null

x

20

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

23

4

x

21

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2x 3

4

22

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

2x 3

4

23

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

23

4

5

tail

24

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

23

4

5

6

tail

25

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

23

4

5

6 7

26

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

23

4

5

6 7

8

27

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

23

4

5

6 7

8

9

28

public class Josephus { private static class Node { int val; Node next; }

public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]);

Node head = new Node(); head.val = 1; head.next = head; Node tail = head;

for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; }

Josephus Problem: Build the Circular Linked List

1

head

tail

23

4

5

6 7

8

9

Done Building

29

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9N

9

M

5

30

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

N

9

M

5

31

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

N

9

M

5

32

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

i

1

N

9

M

5

33

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

i

1

N

9

M

5

34

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

i

2

N

9

M

5

35

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

i

2

N

9

M

5

36

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

i

3

N

9

M

5

37

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

i

3

N

9

M

5

38

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

i

4

N

9

M

5

39

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

i

4

N

9

M

5

40

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 9

x

i

5

N

9

M

5

41

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 95

x

N

9

M

5

42

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

1

23

4

5

6 7

8

9

% java Josephus 5 95

x

N

9

M

5

5 is effectively deleted

43

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

1

23

4

6 7

8

9

% java Josephus 5 95

x

N

9

M

5

44

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

i

1

N

9

M

5

1

23

4

6 7

8

9x

% java Josephus 5 95

45

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

i

1

N

9

M

5

1

23

4

6 7

8

9x

% java Josephus 5 95

46

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

i

2

N

9

M

5

1

23

4

6 7

8

9x

% java Josephus 5 95

47

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

i

2

N

9

M

5

1

23

4

6 7

8

9x

% java Josephus 5 95

48

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

i

3

N

9

M

5

1

23

4

6 7

8

9x

% java Josephus 5 95

49

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

i

3

N

9

M

5

1

23

4

6 7

8

9x

% java Josephus 5 95

50

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

i

4

N

9

M

5

1

23

4

6 7

8

9x

% java Josephus 5 95

51

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

i

4

N

9

M

5

1

23

4

6 7

8

9x

% java Josephus 5 95

52

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

i

5

N

9

M

5

1

23

4

6 7

8

9x

% java Josephus 5 95

53

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

% java Josephus 5 95 1

N

9

M

5

1

23

4

6 7

8

9x

54

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

% java Josephus 5 95 1

N

9

M

5

1

23

4

6 7

8

9x

55

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

% java Josephus 5 95 1

N

9

M

5

23

4

6 7

8

9x

56

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

% java Josephus 5 95 1 7

N

9

M

5

23

4

6 7

8

9x

57

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

% java Josephus 5 95 1 7 4

N

9

M

5

23

6

8

9x

58

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

% java Josephus 5 95 1 7 4 3

N

9

M

5

2

6

8

9x

59

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

% java Josephus 5 95 1 7 4 3 6

N

9

M

5

2

8

9x

60

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

% java Josephus 5 95 1 7 4 3 6 9

N

9

M

5

2

8

x

61

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

% java Josephus 5 95 1 7 4 3 6 9 2

N

9

M

5

8

x

62

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

% java Josephus 5 95 1 7 4 3 6 9 2

N

9

M

5

8

x

63

Josephus Problem: Kill Off Every Mth Person

Node x = tail;while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next;}System.out.println(x.val);

% java Josephus 5 95 1 7 4 3 6 9 2 8

N

9

M

5

8

x

Survivor