<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                > 算法為自創,非微信官方算法(僅供參考);如測出有問題,請及時反饋。感謝 原文:[http://flc.ren/2018/04/701.html](http://flc.ren/2018/04/701.html) ### CHANGELOG * * * **2018-04-07** * 修復最后一個紅包輸出未保留2位數 * 修復領取的紅包金額低于最小紅包限制 ~~~ <?php /** * 紅包分配算法 * * @example * $coupon = new Coupon(200, 5); * $res = $coupon->handle(); * print_r($res); * * @author Flc <2018-04-06 20:09:53> * @see http://flc.ren | http://flc.io | https://github.com/flc1125 */ class Coupon { /** * 紅包金額 * * @var float */ protected $amount; /** * 紅包個數 * * @var int */ protected $num; /** * 領取的紅包最小金額 * * @var float */ protected $coupon_min; /** * 紅包分配結果 * * @var array */ protected $items = []; /** * 初始化 * * @param float $amount 紅包金額(單位:元)最多保留2位小數 * @param int $num 紅包個數 * @param float $coupon_min 每個至少領取的紅包金額 */ public function __construct($amount, $num = 1, $coupon_min = 0.01) { $this->amount = $amount; $this->num = $num; $this->coupon_min = $coupon_min; } /** * 處理返回 * * @return array */ public function handle() { // A. 驗證 if ($this->amount < $validAmount = $this->coupon_min * $this->num) { throw new Exception('紅包總金額必須≥'.$validAmount.'元'); } // B. 分配紅包 $this->apportion(); return [ 'items' => $this->items, ]; } /** * 分配紅包 */ protected function apportion() { $num = $this->num; // 剩余可分配的紅包個數 $amount = $this->amount; //剩余可領取的紅包金額 while ($num >= 1) { // 剩余一個的時候,直接取剩余紅包 if ($num == 1) { $coupon_amount = $this->decimal_number($amount); } else { $avg_amount = $this->decimal_number($amount / $num); // 剩余的紅包的平均金額 $coupon_amount = $this->decimal_number( $this->calcCouponAmount($avg_amount, $amount, $num) ); } $this->items[] = $coupon_amount; // 追加分配 $amount -= $coupon_amount; --$num; } shuffle($this->items); //隨機打亂 } /** * 計算分配的紅包金額 * * @param float $avg_amount 每次計算的平均金額 * @param float $amount 剩余可領取金額 * @param int $num 剩余可領取的紅包個數 * * @return float */ protected function calcCouponAmount($avg_amount, $amount, $num) { // 如果平均金額小于等于最低金額,則直接返回最低金額 if ($avg_amount <= $this->coupon_min) { return $this->coupon_min; } // 浮動計算 $coupon_amount = $this->decimal_number($avg_amount * (1 + $this->apportionRandRatio())); // 如果低于最低金額或超過可領取的最大金額,則重新獲取 if ($coupon_amount < $this->coupon_min || $coupon_amount > $this->calcCouponAmountMax($amount, $num) ) { return $this->calcCouponAmount($avg_amount, $amount, $num); } return $coupon_amount; } /** * 計算分配的紅包金額-可領取的最大金額 * * @param float $amount * @param int $num */ protected function calcCouponAmountMax($amount, $num) { return $this->coupon_min + $amount - $num * $this->coupon_min; } /** * 紅包金額浮動比例 */ protected function apportionRandRatio() { // 60%機率獲取剩余平均值的大幅度紅包(可能正數、可能負數) if (rand(1, 100) <= 60) { return rand(-70, 70) / 100; // 上下幅度70% } return rand(-30, 30) / 100; // 其他情況,上下浮動30%; } /** * 格式化金額,保留2位 * * @param float $amount * * @return float */ protected function decimal_number($amount) { return sprintf('%01.2f', round($amount, 2)); } } // 例子 $coupon = new Coupon(200, 5, 30); $res = $coupon->handle(); print_r($res); ~~~ 運行結果:[http://www.dooccn.com/php7/#i...](http://www.dooccn.com/php7/#id/a512e9be939af0935c8da2ed933a9ee8)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看