<?
//堆排序应用
class heapsort
{
var $a;
function setarray($a)//取得数组
{
$this->a=$a;
}
function runvalue($b,$c)//$a 代表数组,$b代表排序堆,$c代表结束点,
{
while($b<$c)
{
$h1=2*$b;
$h2=(2*$b+1);
if($h1>$c)
break;
elseif($h1==$c)
{
if($this->a[$b]>$this->a[$h1])
{
$t=$this->a[$b];
$this->a[$b]=$this->a[$h1];
$this->a[$h1]=$t;
$la=1;
}
else
$la=1;
}
elseif(($this->a[$b]>$this->a[$h1])||($this->a[$b]>$this->a[$h2]))
{
if($this->a[$h1]>=$this->a[$h2])
{
$t=$this->a[$h2];
$this->a[$h2]=$this->a[$b];
$this->a[$b]=$t;
$b=$h2;
}
else
{
$t=$this->a[$h1];
$this->a[$h1]=$this->a[$b];
$this->a[$b]=$t;
$b=$h1;
}
}
else
$la=1;
if($la==1)
break;
}
}
function getarray()
{
$all=count($this->a);
$b=Floor(($all-1)/2);
for($i=$b;$i>=1;$i--)//先将数组建立成堆
{
$this->runvalue($i,($all-1));
}
for($i=1;$i<$all;$i++)
{
$a1=($all-$i);
if($i==1)
{
$t=$this->a[1];
$this->a[1]=$this->a[$a1];
$this->a[$a1]=$t;
}
else
{
$end=($all-$i);
$this->runvalue(1,$end);
$t=$this->a[1];
$this->a[1]=$this->a[$end];
$this->a[$end]=$t;
}
}
return $this->a;
}
}
//////
class sortarr
{
var $a;
function setarray($a)//取得数组
{
$this->a=$a;
}
function runvalue($i)
{
$max=$this->a[$i];
$id=$i;
for($j=($i+1);$j<count($this->a);$j++)
{
if($this->a[$j]>$max)
{
$max=$this->a[$j];
$id=$j;
}
}
if($id!=$i)
{
$t=$this->a[$id];
$this->a[$id]=$this->a[$i];
$this->a[$i]=$t;
}
}
function getarray()
{
for($i=1;$i<(count($this->a)-1);$i++)
$this->runvalue($i);
return $this->a;
}
}
//////
$s=microtime();
$st=explode(' ',$s);
$st1=$st[0];
$st2=$st[1];
//////
$v=10000;//排序数组长度
$brr[0]=0;
for($i=1;$i<$v;$i++)
{
$brr[$i]=rand();
}
$check=2;//1 stand for heapsort 2 stand for another sort
echo'after sort!!<br>';
if($check==1)
{
$arr=new heapsort;
$arr->setarray($brr);
$ok=$arr->getarray();
for($i=1;$i<$v;$i++)
{
$j=((($i+1)>($v-1))?($v-1):($i+1));
/*
if($ok[$j]<$ok[$i])
echo'<font color=red>'.$ok[$i].'</font><br>';
else
echo$ok[$i].'<br>';*/
}
}
elseif($check==2)
{
$arr=new sortarr;
$arr->setarray($brr);
$ok=$arr->getarray();
for($i=1;$i<$v;$i++)
{
$j=((($i+1)>($v-1))?($v-1):($i+1));/*
if($ok[$j]<$ok[$i])
echo'<font color=red>'.$ok[$i].'</font><br>';
elseif($ok[$j]>$ok[$i])
echo'<font color=green>'.$ok[$i].'</font><br>';
else
echo$ok[$i].'<br>';*/
}
}
elseif($check==3)
{
sort($brr);
$ok=$brr;
for($i=1;$i<$v;$i++)
{
$j=((($i+1)>($v-1))?($v-1):($i+1));/*
if($ok[$j]<$ok[$i])
echo'<font color=red>'.$ok[$i].'</font><br>';
elseif($ok[$j]>$ok[$i])
echo'<font color=green>'.$ok[$i].'</font><br>';
else
echo$ok[$i].'<br>';*/
}
}
else
{
echo'参数输入错误!!<br>';
}
//////
$s=microtime();
$st=explode(' ',$s);
$sta=$st[0];
$stb=$st[1];
$ss1=$sta-$st1;
$ss2=$stb-$st2;
if($check==1)
$word='堆排序';
elseif($check==2)
$word='常规排序';
elseif($check==3)
$word='普通排序';
else
$word='无排序';
echo$word.'对具有'.$v.'个元素的数组排序,消耗了'.($ss2+$ss1).'秒时间';
//////
?>
下面的就是生成EAN_13标准的条码的PHP方法,需要php+gd 环境
function EAN_13($code) {
//一个单元的宽度
$lw = 2;
//条码高
$hi = 100;
// the guide code is no coding,is used to show the left part coding type//
// Array guide is used to record the EAN_13 is left part coding type//
$Guide = array(1=>'AAAAAA','AABABB','AABBAB','ABAABB','ABBAAB','ABBBAA','ABABAB','ABABBA','ABBABA');
$Lstart ='101';
$Lencode = array("A" => array('0001101','0011001','0010011','0111101','0100011','0110001','0101111','0111011','0110111','0001011'),
"B" => array('0100111','0110011','0011011','0100001','0011101','0111001','0000101','0010001','0001001','0010111'));
$Rencode = array('1110010','1100110','1101100','1000010','1011100',
'1001110','1010000','1000100','1001000','1110100');
$center = '01010';
$ends = '101';
if ( strlen($code) != 13 )
{ die("UPC-A Must be 13 digits."); }
$lsum =0;
$rsum =0;
for($i=0;$i<(strlen($code)-1);$i++)
{
if($i % 2)
{
// $odd += $ncode[$x]
$lsum +=(int)$code[$i];
}else{
$rsum +=(int)$code[$i];
}
}
$tsum = $lsum*3 + $rsum;
if($code[12] != (10-($tsum % 10)))
{
die("the code is bad!");
}
// echo $Guide[$code[0]];
$barcode = $Lstart;
for($i=1;$i<=6;$i++)
{
$barcode .= $Lencode [$Guide[$code[0]][($i-1)]] [$code[$i]];
}
$barcode .= $center;
for($i=7;$i<13;$i++)
{
$barcode .= $Rencode[$code[($i)]] ;
}
$barcode .= $ends;
$img = ImageCreate($lw*95+60,$hi+30);
$fg = ImageColorAllocate($img, 0, 0, 0);
$bg = ImageColorAllocate($img, 255, 255, 255);
ImageFilledRectangle($img, 0, 0, $lw*95+60, $hi+30, $bg);
$shift=10;
for ($x=0;$x<strlen($barcode);$x++) {
if (($x<4) || ($x>=45 && $x<50) || ($x >=92))
{
$sh=10;
} else {
$sh=0;
}
if ($barcode[$x] == '1')
{
$color = $fg;
} else {
$color = $bg;
}
ImageFilledRectangle($img, ($x*$lw)+30,5,($x+1)*$lw+29,$hi+5+$sh,$color);
}
/* Add the Human Readable Label */
ImageString($img,5,20,$hi+5,$code[0],$fg);
for ($x=0;$x<6;$x++) {
ImageString($img,5,$lw*(8+$x*6)+30,$hi+5,$code[$x+1],$fg);
ImageString($img,5,$lw*(53+$x*6)+30,$hi+5,$code[$x+7],$fg);
}
// ImageString($img,4,$lw*95+17,$hi-5,$code[12],$fg);
/* Output the Header and Content. */
header("Content-Type: image/png");
ImagePNG($img);
}
EAN_13('6901028055048');
?>
<?php
/**
* 要解决的数学问题 :算出C(a,1) * C(b, 1) * ... * C(n, 1)的组合情况,其中C(n, 1)代表从n个元素里任意取一个元素
*
* 要解决的实际问题样例:某年级有m个班级,每个班的人数不同,现在要从每个班里抽选一个人组成一个小组,
* 由该小组来代表该年级参加学校的某次活动,请给出所有可能的组合
*/
/* ################################### 开始计算 ################################### */
/**
* 需要进行排列组合的数组
*
* 数组说明:该数组是一个二维数组,第一维索引代表班级编号,第二维索引代表学生编号
*/
$CombinList = array(1 => array("Student10", "Student11"),
2 => array("Student20", "Student21", "Student22"),
3 => array("Student30"),
4 => array("Student40", "Student41", "Student42", "Student43"));
/* 计算C(a,1) * C(b, 1) * ... * C(n, 1)的值 */
$CombineCount = 1;
foreach($CombinList as $Key => $Value)
{
$CombineCount *= count($Value);
}
$RepeatTime = $CombineCount;
foreach($CombinList as $ClassNo => $StudentList)
{
// $StudentList中的元素在拆分成组合后纵向出现的最大重复次数
$RepeatTime = $RepeatTime / count($StudentList);
$StartPosition = 1;
// 开始对每个班级的学生进行循环
foreach($StudentList as $Student)
{
$TempStartPosition = $StartPosition;
$SpaceCount = $CombineCount / count($StudentList) / $RepeatTime;
for($J = 1; $J <= $SpaceCount; $J ++)
{
for($I = 0; $I < $RepeatTime; $I ++)
{
$Result[$TempStartPosition + $I][$ClassNo] = $Student;
}
$TempStartPosition += $RepeatTime * count($StudentList);
}
$StartPosition += $RepeatTime;
}
}
/* 打印结果 */
echo "<pre>";
print_r($Result);
?>