ivoryworks #5219(2008/01/08 03:24 GMT) [ PHP ] Rating0/0=0.00
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
<?php function CreateAmida($org) { if (!ctype_alnum($org) || count(array_unique(str_split($org))) != count(str_split($org))) { return NULL; } $bridge_num = strlen($org)-1; $p = $org; $step = array(); while (1) { $chg = FALSE; $step_buf = array_fill(0, $bridge_num, ' '); for ($i = 0; $i < $bridge_num; $i++) { if ($p[$i] > $p[$i+1]) { $sub = array($p[$i]=>$p[$i+1], $p[$i+1]=>$p[$i]); $p = strtr($p, $sub); $step_buf[$i] = '-'; $i++; $chg = TRUE; } } if ($chg) { $step[] = '|'.implode('|', $step_buf).'|'; } else { break; } } $res = implode(' ', str_split($p))."\n"; $res .= implode("\n", array_reverse($step))."\n"; $res .= implode(' ', str_split($org))."\n"; return $res; } ?>
Rating0/0=0.00-0+
[ reply ]
ivoryworks
#5219()
[
PHP
]
Rating0/0=0.00
数字のみでなく英字も受け付ける関数仕様になっています。
また、あみだくじの特性上、重複した文字が含まれるリストを受け付けません。
リストをソート(変則的なバブルソート)する過程を記録し、過程を逆転させたものをあみだくじのパターンとするロジックです。
ex)
echo '<pre>'.CreateAmida('9876543210').'</pre>';
Rating0/0=0.00-0+
[ reply ]