微信红包java算法代码 java微信红包实现( 四 )


{
if(false == is_int($this-num) || $this-num = 0)
{
return false;
}
if(false == is_numeric($this-totalMoney) || $this-totalMoney = 0) {
return false;
}
//均值
$avgMoney = $this-totalMoney / 1.0 / $this-num;
//均值?于最?值
if($avgMoney$this-minMoney )
{
return false;
}
return true;
}
/**
* 获取剩余?额
* @access public
public function getLeftMoney()
{
return $this-leftMoney;
}
/**
* 随机红包?成函数 。三?函数 。[(1,0.01),($num/2,$avgMoney),($num,0.01)] * @param mixed $x,1 = $x = $this-num;
* @access public
* @return void
*/
public function fx($x)
{
if(false == $this-isCanBuilder())
{
return 0;
}
if($x1 || $x$this-num)
{
return 0;
}
$x1 = 1;
$y1 = $this-minMoney;
//中间点
$x2 = ceil($this-num / 1.0 / 2);
//我的峰值
$y2 = $this-maxMoney;
//最后点
$x3 = $this-num;
$y3 = $this-minMoney;
//当x1,x2,x3都是1的时候(竖线)
if($x1 == $x2$x2 == $x3)
{
// '/_\'三?形状的线性?程
//'/'部分
if($x1 != $x2$x = $x1$x = $x2)
{
、 $y = 1.0 * ($x - $x1) / ($x2 - $x1) * ($y2 - $y1) + $y1; return number_format($y, 2, '.', '');
}
//'\'形状
if($x2 != $x3$x = $x2$x = $x3)
{
$y = 1.0 * ($x - $x2) / ($x3 - $x2) * ($y3 - $y2) + $y2; return number_format($y, 2, '.', '');
}
return 0;
}
/**
* 格式化修红包数据
*
* @param mixed $leftMoney
* @param array $data
* @access public
* @return void
*/
private function format($leftMoney,array $data)
{
//不能发随机红包
if(false == $this-isCanBuilder())
{
return array($leftMoney,$data);
}
//红包剩余是0
if(0 == $leftMoney) //?需修数据
return array($leftMoney,$data);
}
//数组为空
if(count($data)1)
{
return array($leftMoney,$data);
}
//如果是可以有剩余,并且$leftMoney0
if('Can_Left' == $this-formatType
$leftMoney0)
{
return array($leftMoney,$data);
}
// 如果还有余钱,则尝试加到?红包?,如果加不进去,则尝试下?个 。while($leftMoney0)
{
$found = 0;
foreach($data as $key = $val)
{
//减少循环优化
if($leftMoney = 0)
{
break;
}
//预判
$afterLeftMoney = (double)$leftMoney - 0.01;
$afterVal = (double)$val + 0.01;
if( $afterLeftMoney = 0$afterVal = $this-maxMoney)
{
$found = 1;
$data[$key] = number_format($afterVal,2,'.','');
$leftMoney = $afterLeftMoney;
//精度
}
}
//如果没有可以加的红包,需要结束,否则死循环
//也就是会出现每个红包不分钱的情况,?如红包都已经最?值 。这时必须在分的时候给予标志,防?死循环 。if($found == 0)
{
break;
}
}
//如果$leftMoney0,说明?成的红包超过预算了,需要减少部分红包?额
while($leftMoney0)
{
$found = 0;
foreach($data as $key = $val)
{
if($leftMoney = 0)
{
break;
}
//预判
$afterLeftMoney = (double)$leftMoney + 0.01;
$afterVal = (double)$val - 0.01;
if( $afterLeftMoney = 0$afterVal = $this-minMoney)
{
$found = 1;
$data[$key] = number_format($afterVal,2,'.','');
$leftMoney = $afterLeftMoney;
$leftMoney = number_format($leftMoney,2,'.','');
}
}
//如果?个减少的红包都没有的话,需要结束,否则死循环
if($found == 0)
{
}
}
return array($leftMoney,$data);
}

推荐阅读