-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJosef.php
64 lines (54 loc) · 1.02 KB
/
Josef.php
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
class Josef{
public $first = null;
public function add($num){
$cur = null;
for ($i = 1; $i <= $num ; $i++) {
$boy = new Node($i);
if($i == 1){
$this->first = $boy;
$boy->next = $this->first;
}else{
$cur->next = $boy;
$boy->next = $this->first;
}
$cur = $boy;
}
}
public function countBoy($n,$k){
$helper = $this->first;
while (1) {
if($helper->next == $this->first){
break;
}
$helper = $helper->next;
}
for ($i = 0; $i < $n; $i++) {
$this->first = $this->first->next;
$helper = $helper->next;
}
while (1) {
if ($helper == $this->first) {
break;
}
for ($i = 0; $i < $k - 1; $i++) {
$this->first = $this->first->next;
$helper = $helper->next;
}
var_dump($this->first->no);
$this->first = $this->first->next;
$helper->next = $this->first;
}
}
}
class Node{
public $no;
public $next = null;
public function __construct($no)
{
$this->no = $no;
}
}
$josef = new Josef;
$josef->add(8);
$josef->countBoy(3,2);