function sa($members) { $T = 100.0; $now_temp = 10000; $n = count($members); $status = 0; $best = 1000000000; while ($T > 0) { do { $t1 = rand(0, $n - 1); $t2 = rand(0, $n - 1); } while ($t1 == $t2); $now = sqr(abs($members[$t1]['chosen1'] - $t1)) + sqr(abs($members[$t2]['chosen1'] - $t2)); $new = sqr(abs($members[$t1]['chosen1'] - $t2)) + sqr(abs($members[$t2]['chosen1'] - $t1)); if ($new < $now || lcg_value() <= exp(($now - $new) / $T)) { $temp = $members[$t1]; $members[$t1] = $members[$t2]; $members[$t2] = $temp; $status += $new - $now; if ($status < $best) { $res = $members; } } $T -= 0.02; } return $res; }
/** * * @param $bonus_total 红包总额 * @param $bonus_count 红包个数 * @param $bonus_max 每个小红包的最大额 * @param $bonus_min 每个小红包的最小额 * @return 存放生成的每个小红包的值的一维数组 */ function getBonus($bonus_total, $bonus_count, $bonus_max, $bonus_min) { $result = array(); $average = $bonus_total / $bonus_count; $a = $average - $bonus_min; $b = $bonus_max - $bonus_min; // //这样的随机数的概率实际改变了,产生大数的可能性要比产生小数的概率要小。 //这样就实现了大部分红包的值在平均数附近。大红包和小红包比较少。 $range1 = sqr($average - $bonus_min); $range2 = sqr($bonus_max - $average); for ($i = 0; $i < $bonus_count; $i++) { //因为小红包的数量通常是要比大红包的数量要多的,因为这里的概率要调换过来。 //当随机数>平均值,则产生小红包 //当随机数<平均值,则产生大红包 if (rand($bonus_min, $bonus_max) > $average) { // 在平均线上减钱 $temp = $bonus_min + xRandom($bonus_min, $average); $result[$i] = $temp; $bonus_total -= $temp; } else { // 在平均线上加钱 $temp = $bonus_max - xRandom($average, $bonus_max); $result[$i] = $temp; $bonus_total -= $temp; } } // 如果还有余钱,则尝试加到小红包里,如果加不进去,则尝试下一个。 while ($bonus_total > 0) { for ($i = 0; $i < $bonus_count; $i++) { if ($bonus_total > 0 && $result[$i] < $bonus_max) { $result[$i]++; $bonus_total--; } } } // 如果钱是负数了,还得从已生成的小红包中抽取回来 while ($bonus_total < 0) { for ($i = 0; $i < $bonus_count; $i++) { if ($bonus_total < 0 && $result[$i] > $bonus_min) { $result[$i]--; $bonus_total++; } } } return $result; }
<?php function sqr($num) { return $num * $num; } print sqr(3);
<html> <body> <?php function sqr($par) { return $par * $par; } function inc(&$par) { ++$par; } $txt1 = "Hello World!"; $txt2 = "What a nice day!"; $txt = $txt1 . " " . $txt2; echo $txt . "<br>" . strlen($txt) . '\\n' . strpos($txt, "World") . '<br>'; echo sqr(5) . '<br>'; $par = 5; inc($par); echo $par; ?> </body> </html>
<?php require 'functions.php'; print "sqr(3)=" . sqr(3);
use const nspl\args\nonEmpty; use const nspl\args\arrayAccess; use function nspl\args\withKeys; use function nspl\args\withMethod; use function nspl\args\expects; use function nspl\args\expectsAll; use function nspl\args\expectsOptional; use function nspl\args\expectsToBe; // 1. Specify scalar parameter type function sqr($x) { expects(numeric, $x); return $x * $x; } try { sqr('hello world'); } catch (\InvalidArgumentException $e) { echo $e->getMessage() . "\n"; } // 2. Specify several types function first($sequence) { expects([nonEmpty, arrayAccess, string], $sequence); return $sequence[0]; } try { first(12); } catch (\InvalidArgumentException $e) { echo $e->getMessage() . "\n"; } // 3. Specify several parameters of the same type
function de($method, $s) { switch ($method) { case 'snd': break; case 'bin': $s = bin_de($s); break; case 'dec': $s = dec_de($s); break; case 'oct': $s = oct_de($s); break; case 'hex': $s = hex_de($s); break; case 'rot': $s = rotate($s, 26 - $_POST['rot'], 10 - $_POST['nrot']); break; case 'url': $s = $_POST['url_raw'] == 'on' ? rawurldecode($s) : urldecode($s); break; case 'ur2': $s = urldecode($s); break; case 'raw': break; case 'stmwth': break; case 'rev': $s = str_rev($s); break; case 'spe': $s = html_entity_decode($s); break; case 'hen': break; case 'nbase': $s = base_conv($s, 1); break; case 'base': $s = base_de($s); break; case 'md5': addmsg(INFO, '<a href="http://gdataonline.com/seekhash.php" target="_blank">http://gdataonline.com/seekhash.php</a>'); break; case 'crypt': break; case 'stu': break; case 'crv': $s = case_rev($s); break; case 'stl': break; case 'ucw': break; case 'bbs': break; case 'bsd': break; case 'srt': $s = mysort($s, 1); break; case 'rpt': break; case 'unq': $s = uniq($s, 1); break; case 'rf': $s = sqr($s, 1); break; case 'pcr': break; case 'acc': $s = accumulation($s, 1); break; case 'uue': $s = convert_uudecode($s); break; case 'det': break; case 'ttb': break; case 'mut': break; case 'sha1': break; case 'msk': break; case 'ref': $s = sqr_reflect($s); break; case 'crc16': break; case 'crc32': break; case 'sta': break; case 'rep': $s = gen_rep_de($s); break; case 'cor': break; case 'pcm': break; case 'ascii': $s = ASCIIFilter_de($s); break; case 'ave': break; case 'miv': break; case 'mmtp': break; case 'mro': $s = matrix_rotate($s, 1); break; case 'mtr': $s = matrix_transpose($s); break; case 'cac': break; case 'ctr': break; case 'swd': break; case 'che': break; case 'key': $s = key_xor($_POST['key'], $s); break; case 'bre': $s = bit_rev($s); break; case 'bod': $s = bitorder_de($_POST['order'], $s); break; case 'tra': $s = tran(12 - $_POST['transpose'], $s, 1); break; case 'quot': break; default: addmsg(ERR, 'Undefined Method: ' . $method); } return $s; }