哈哈~初一也是要發文~
最近又跑出一個問題,說是上一篇中有提到因為雙方的策略一致,導致勝率是佔各半(沒有平手,因為沒有分數一樣的問題)。但是如果兩個人的策略不一樣的話會怎樣呢~
我們先設總共有100家店,我和敵人個從商店街的兩邊開始走,我用一種策略,他用一種策略,跑10,000次後,我方勝率會是多少...
100家的話就會有100x100=10,000種。要跑10,000次的話,要循環100,000,000次,真的這樣跑的話,四天跑不掉...所以我打算敵我方的策略只做50家就好,這樣時間就可以控制在一天就好...程式如下:
<?php
if (isset($argv))
for ($i=1;$i<count($argv);$i++)
{
$it = explode("=",$argv[$i]);
$_GET[$it[0]] = $it[1];
}
$n="\n";
$brn="<br />\n";
date_default_timezone_set("Asia/Taipei");
//設定變數
//mode=0 無變數
//mode=1 凋謝速率
//mode=2 員工貢獻度
//mode=3 競爭
//mode=4 參考
//mode=5 I can see you, you can see me...
if(isset($_GET['debug'])) $debug=$_GET['debug'];
else $debug=1;//顯示步驟
if(isset($_GET['from'])) $from=$_GET['from'];
else $from=0;
if(isset($_GET['to'])) $to=$_GET['to'];
else $to=100;
if(isset($_GET['gap'])) $gap=$_GET['gap'];
else $gap=0.1;
if(isset($_GET['army_from'])) $army_from=$_GET['army_from'];
else $army_from=0;
if(isset($_GET['army_to'])) $army_to=$_GET['army_to'];
else $army_to=100;
if(isset($_GET['army_gap'])) $army_gap=$_GET['army_gap'];
else $army_gap=0.1;
if(isset($_GET['choice_time'])) $choice_time=$_GET['choice_time'];
else $choice_time=1;
if(isset($_GET['limit'])) $limit=$_GET['limit'];
else $limit=100;
if(isset($_GET['loop_num'])) $loop_num=$_GET['loop_num'];
else $loop_num=50;
printf("date,me\army,=>");
for($a=$army_from;$a<=$army_to;$a=$a+$army_gap){
printf(",$a");
}
printf("$n");
for($k=$from;$k<=$to;$k=$k+$gap){
$percent=$k;
$thedate=date("m-d H:i");
printf("%s,%.1f,=>", $thedate, $percent);
for($p=$army_from;$p<=$army_to;$p=$p+$army_gap){
$army_percent=$p;
$win=0;
for($j=0;$j<$loop_num;$j++){
//變數設定
$max_num=0;
$max_percent=$limit*$percent/100;
$army_max_percent=$limit*(100-$army_percent)/100;
$percent_num=0;
$army_percent_num=0;
$choice=0;
$army_choice=0;
$time=0;
$army_time=0;
$thisnum=range(1,$limit,1);//(小,大,差)
shuffle($thisnum);//洗牌
for($i=0;$i<$limit;$i++){
if($i>=$max_percent and $i-1<$max_percent) $time=$choice_time;
if($limit-$i>=$army_max_percent and $limit-$i-1<$army_max_percent) $army_time=$choice_time;
$num=$thisnum[$i];//我方此家分數
$army_num=$thisnum[$limit-$i-1];//敵方此家分數
if($max_num<=$num) $max_num=$num;//找尋最大值
if($max_num<=$army_num) $max_num=$army_num;
if($percent_num<$num) $percent_num=$num;
if($army_percent_num<$army_num) $army_percent_num=$army_num;
if($time>0 and $num>=$percent_num and $num>=$army_percent_num) {
$choice=$choice+$num;
$time=$time-1;
$thisnum[$i]=0;
}//拿走
if($army_time>0 and $army_num>=$army_percent_num and $army_num>=$percent_num) {
$army_choice=$army_choice+$army_num;
$army_time=$army_time-1;
$thisnum[$limit-$i-1]=0;
}//敵方拿走
if($debug>0) echo $i+1, " ", $num, " ", $army_num, " ", $percent_num, " ", $army_percent_num, " ", $time, " ", $army_time, $n;//顯示資訊
}
if($debug>0){
echo "Choice: ", $choice, ",", $army_choice, $n;
echo "Max: ", $max_num, $n;
}
if($max_num==$choice) $best++;
if($choice==0) $fail++;
if($max_num>$choice and $choice>0) $fine++;
if($choice>$army_choice) $win++;
}
printf(",%.6f", $win/$loop_num);
}
echo ("$n");
}
?>
還有什麼變數呢...?
如果我很壞,只讓敵人告訴我他那一家的分數,但是我卻不告訴他呢?這樣的話一定是對我有利。對我有弊的話應該就是告訴他我這家的分數,但是她不告訴我吧...最慘的是,我不告訴她,他也不告訴我,這樣就是在沒有網路的世代會做的事...XD
程式如下:
我看得到對方,但是對方看不到我那一家的分數時的程式。其實就只是改一下判斷標準罷了...如下:
mode=5 I can see you, you can't see me...
if($time>0 and $num>=$percent_num and $num>=$army_percent_num) {
$choice=$choice+$num;
$time=$time-1;
$thisnum[$i]=0;
}//拿走
if($army_time>0 and $army_num>=$army_percent_num) {
$army_choice=$army_choice+$army_num;
$army_time=$army_time-1;
$thisnum[$limit-$i-1]=0;
}//敵方拿走
第三個是別人看得到我,但是我看不到別人的程式~
mode=5 I can't see you, you can see me...
if($time>0 and $num>=$percent_num) {
$choice=$choice+$num;
$time=$time-1;
$thisnum[$i]=0;
}//拿走
if($army_time>0 and $army_num>=$army_percent_num and $army_num>=$percent_num) {
$army_choice=$army_choice+$army_num;
$army_time=$army_time-1;
$thisnum[$limit-$i-1]=0;
}//敵方拿走
最後一個是我們兩個各自看不到對方的程式~
mode=5 I can't see you, you can't see me too...
if($time>0 and $num>=$percent_num) {
$choice=$choice+$num;
$time=$time-1;
$thisnum[$i]=0;
}//拿走
if($army_time>0 and $army_num>=$army_percent_num) {
$army_choice=$army_choice+$army_num;
$army_time=$army_time-1;
$thisnum[$limit-$i-1]=0;
}//敵方拿走
明天看結果吧~