<?php
$file = "task1.txt";$fp = fopen($file, "r");
$content = fread($fp, filesize($file));
fclose($fp);//parse $content
$tmp = explode("\n", $content);
$length1 = count($tmp);$data = array();
$invalid = 0;
for($i=0, $j=0; $i<$length1; $i++)
        if(strlen($tmp[$i]) > 10)
        {
                $tt = explode(",", $tmp[$i]);
                if($tt[0] == -1) { $invalid ++; continue; }
                if($j < 1 || $tt[0] != $data[$j-1][0])                //filter repeated numbers
                {
                        $data[$j] = $tt;
                        $j++;
                }
        }$length2 = count($data);//make a int_array;
$int_array = array();
$ab_array = array("A"=>0, "B"=>0);
$A_array = array();                        //stat of max continuous repeated number
$B_array = array();$flag = 1;
$A_repeated = 0;
$B_repeated = 0;for($i=0; $i<$length2; $i++)
{
        $index = $data[$i][0];
        $int_array[$index-1] += 1;
        if($int_array[$index-1] %2 ==0)
        {
                if($flag)        { 
                        //end of repeated A
                        $A_array[$A_repeated] ++;
                        $A_repeated =0;
                        $flag = 0; 
                }
                        $ab_array['B']++; 
                        $B_repeated++;
        }else {
                if(!$flag)        { 
                        //end of repeated B
                        $B_array[$B_repeated] ++;
                        $B_repeated =0;
                        $flag = 1; 
                }
                        $ab_array['A']++; 
                        $A_repeated++;
        }        
}                if($flag)        { 
                        //end of repeated A
                        $A_array[$A_repeated] ++;
                }else 
                { 
                        //end of repeated B
                        $B_array[$B_repeated] ++;
                }$result = "";
for($i=0; $i<75; $i++)
        $result .= sprintf("%d, %.2f", $i+1, $int_array[$i]/$length2*100)."%<br>";//echo $result;
echo "总计:". $length1. "<br />";
echo "无效:". $invalid. "<br />";
echo "重复: ". sprintf("%d", $length1 - $length2). "<br />";
echo "有效且不重复:". sprintf("%d", $length2). "<br />";echo "--------------------------------------------------------<br /> A = 奇; B = 偶 <br /> <br />";
echo "A: ". sprintf("%d, %.2f", $ab_array['A'], $ab_array['A'] / ($ab_array['A'] + $ab_array['B'])*100). "%<br />";
echo "B: ". sprintf("%d, %.2f", $ab_array['B'], $ab_array['B'] / ($ab_array['A'] + $ab_array['B'])*100). "%<br />";echo "--------------------------------------------------------<br />";
$total = 0;
$size = count($A_array);
foreach($A_array as $repeated => $max)
{
        echo "A[". $repeated ."] = ".sprintf("%d 次, %.2f", $max, 100*$repeated*$max/$ab_array['A'])."%<br>";
        $total +=$max*$repeated;
}echo "--------------------------------------------------------<br />";
echo "**统计校验: ".$total."<br />";
echo "--------------------------------------------------------<br />";
$total = 0;
foreach($B_array as $repeated => $max)
{
        echo "B[". $repeated ."] = ".sprintf("%d 次, %.2f", $max, 100*$repeated*$max/$ab_array['B'])."%<br>";
        $total +=$max*$repeated;
}echo "--------------------------------------------------------<br />";
echo "**统计校验: ".$total."<br />";
echo "--------------------------------------------------------<br />";