-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoin.php
235 lines (195 loc) · 4.31 KB
/
coin.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
function coin()
{
return rand(1, 2);
}
function coin_side()
{
return array(1 => "H", 2 => "T");
}
function coin_h()
{
return "H";
}
function coin_t()
{
return "T";
}
function throw_coin_untill_H()
{
$counter = 1;
while (true) {
$index = coin();
$c = coin_side();
$side = $c[$index];
echo $side;
$counter += 1;
if ($counter % 16 == 0) {
echo "\n";
}
if ($side == "H") {
break;
}
}
}
function throw_coins($coin_f_collection, $number = 1, $pre = "")
{
$number -= 1;
$temp = $coin_f_collection;
foreach ($coin_f_collection as $coin_function) {
if ($number > 0) {
$pre .= $coin_function();
throw_coins($temp, $number, $pre);
$pre = substr($pre, 0, -1);
} else {
echo $pre . $coin_function() . "\n";
}
}
}
function throw_coins_no_recusive($coin_f_collection, $number = 1, $pre = "")
{
$stack = array();
$temp = count ( $coin_f_collection);
$number -= 1;
$i = 0;
$con = array();
while(true) {
while ($i < $temp) {
$coin_function = $coin_f_collection[$i];
if ($number > 0) {
$pre .= $coin_function();
//index points to next item
array_push($stack, array('n' => $number, 'r' => substr($pre, 0, -1), 'index'=>$i+1));
$number -= 1;
$i = 0;
} else {
$h = $pre . $coin_function();
$con[] = $h;
echo $h . "\n";
$i += 1;
}
}
$item = array_pop($stack);
if (!$item) {return $con; }
$number = $item['n'];
$i = $item['index']; //important,
$pre = $item['r'];
}
//echo "end\n";
return $con;
}
function coin_a()
{
return "a";
}
function coin_b()
{
return "b";
}
function coin_c()
{
return "c";
}
function dice_1()
{
return 1;
}
function dice_2()
{
return 2;
}
function dice_3()
{
return 3;
}
function dice_4()
{
return 4;
}
function dice_5()
{
return 5;
}
function dice_6()
{
return 6;
}
//一小时内, 某十字路口通过的车辆数
function vehicle_at_cross_road($time)
{
}
//抛三枚硬币,至少出现一个正面的集合
function throw3_at_least_one_h()
{
$h = function()
{
return "H";
};
$t =function()
{
return "T";
};
$coin_f_collection = array($h, $t);
$result = throw_coins_no_recusive($coin_f_collection, 3 );
echo "----\n";
foreach ($result as $value) {
if (strpos($value, "H") !== FALSE) {
echo $value ."\n";
}
}
}
//抛三枚硬币,最多出现一个正面的集合
function throw3_at_most_one_h()
{
$h = function()
{
return "H";
};
$t =function()
{
return "T";
};
$coin_f_collection = array($h, $t);
$result = throw_coins_no_recusive($coin_f_collection, 3 );
echo "----at most\n";
foreach ($result as $value) {
if (preg_match("%^[^H]+$|^[H][^H]+$|^[^H]+H[^H]*$%s", $value, $m)) {
echo $value . "\n";
}
}
}
//main
/*
$coin_f_collection = array("coin_h", "coin_t");
throw_coins($coin_f_collection, 1);
echo "\n";
throw_coins($coin_f_collection, 2);
echo "\n";
throw_coins($coin_f_collection, 3);
echo "\n";
$coin_f_collection = array("coin_a", "coin_b", "coin_c");
throw_coins($coin_f_collection, 1);
echo "\n";
$coin_f_collection = array("coin_a", "coin_b", "coin_c");
throw_coins($coin_f_collection,2);
echo "\n";
$coin_f_collection = array("coin_a", "coin_b", "coin_c");
throw_coins($coin_f_collection,3);
echo "\n";
*/
/*$coin_f_collection = array("coin_h", "coin_t");
throw_coins_no_recusive($coin_f_collection, 1 );
echo "\n";
*/
$coin_f_collection = array("coin_h", "coin_t");
throw_coins_no_recusive($coin_f_collection, 5 );
echo "\n";
$coin_f_collection = array("coin_a", "coin_b", "coin_c");
throw_coins_no_recusive($coin_f_collection, 3 );
echo "\n";
$coin_f_collection = array("dice_1", "dice_2", "dice_3","dice_4","dice_5","dice_6");
throw_coins_no_recusive($coin_f_collection, 2 );
echo "\n";
//throw_coin_untill_H();
throw3_at_least_one_h();
throw3_at_most_one_h();