Showing
1 changed file
with
11 additions
and
8 deletions
| @@ -551,6 +551,7 @@ Page({ | @@ -551,6 +551,7 @@ Page({ | ||
| 551 | let finalList = []; //最终用的优惠券组合 | 551 | let finalList = []; //最终用的优惠券组合 |
| 552 | // 计算可叠加的组合 | 552 | // 计算可叠加的组合 |
| 553 | let closestSum = -Infinity; // 最接近或等于输入值的和 | 553 | let closestSum = -Infinity; // 最接近或等于输入值的和 |
| 554 | + let descSum = 0;//满减金额之和 | ||
| 554 | function backtrack(startIndex, currentCombination, currentSum,couponCount) { | 555 | function backtrack(startIndex, currentCombination, currentSum,couponCount) { |
| 555 | // 如果当前和等于输入值,直接返回 | 556 | // 如果当前和等于输入值,直接返回 |
| 556 | if (currentSum == amount) { | 557 | if (currentSum == amount) { |
| @@ -568,14 +569,16 @@ Page({ | @@ -568,14 +569,16 @@ Page({ | ||
| 568 | const obj = validCoupons[i]; | 569 | const obj = validCoupons[i]; |
| 569 | if (obj.accumulationShow == '1') { // 仅考虑可叠加的优惠券 | 570 | if (obj.accumulationShow == '1') { // 仅考虑可叠加的优惠券 |
| 570 | if (currentSum*1 + obj.price*1 > amount) continue; // 如果当前和已经超过输入值,跳过 | 571 | if (currentSum*1 + obj.price*1 > amount) continue; // 如果当前和已经超过输入值,跳过 |
| 571 | - let kyNum = Math.floor(amount/parseFloat(obj.desc)) | ||
| 572 | - if (amount - obj.price*1 >= 0&&(!couponCount[obj.aid]||couponCount[obj.aid] < kyNum)) { | ||
| 573 | - couponCount[obj.aid] = (couponCount[obj.aid] || 0) + 1; // 更新使用次数 | ||
| 574 | - currentCombination.push(obj); // 添加当前数字到组合 | ||
| 575 | - backtrack(i + 1, currentCombination, currentSum*1 + obj.price*1,couponCount); // 递归 | ||
| 576 | - currentCombination.pop(); // 回溯 | ||
| 577 | - couponCount[obj.aid]--; // 回溯时减少使用次数 | ||
| 578 | - } | 572 | + descSum += obj.desc*1; |
| 573 | + let remainingPrice = amount - descSum;//输入金额 - 总满减条件金额 | ||
| 574 | + let kyNum = Math.floor(remainingPrice/parseFloat(obj.desc)) | ||
| 575 | + if (amount - obj.price*1 >= 0&&(!couponCount[obj.aid]||couponCount[obj.aid] < kyNum) && kyNum>-1) { | ||
| 576 | + couponCount[obj.aid] = (couponCount[obj.aid] || 0) + 1; // 更新使用次数 | ||
| 577 | + currentCombination.push(obj); // 添加当前数字到组合 | ||
| 578 | + backtrack(i + 1, currentCombination, currentSum*1 + obj.price*1,couponCount); // 递归 | ||
| 579 | + currentCombination.pop(); // 回溯 | ||
| 580 | + couponCount[obj.aid]--; // 回溯时减少使用次数 | ||
| 581 | + } | ||
| 579 | // 提前退出:如果当前和加上当前数字已经小于输入值,后续的数字也会更小 | 582 | // 提前退出:如果当前和加上当前数字已经小于输入值,后续的数字也会更小 |
| 580 | if (currentSum*1 + obj.price*1 < amount) break; | 583 | if (currentSum*1 + obj.price*1 < amount) break; |
| 581 | } | 584 | } |
-
Please register or login to post a comment