求取给定优惠叠加单元的所有子叠加单元。比如优惠A减3元,优惠B减2元,优惠C减1元,优惠A、B、C都可以叠加,在下单页优惠组件默认选中的是优惠A+优惠B+优惠C(共减6元),组件下拉框中同时存在优惠A+优惠B(共减5元)、优惠A+优惠C(共减4元)、优惠B+优惠C(共减3元)、优惠A(共减3元)、优惠B(共减2元)、优惠C(共减1元),用户可以选择优惠组合并进行切换,以上例子输入ABC,返回AB、AC、BC、A、B、C。
import java.util.ArrayList; import java.util.LinkedList; import java.util.List; /** * 求取给定优惠叠加单元的所有子叠加单元。比如优惠A减3元,优惠B减2元,优惠C减1元,优惠A、B、C都可以叠加, * 在下单页优惠组件默认选中的是优惠A+优惠B+优惠C(共减6元),组件下拉框中同时存在优惠A+优惠B(共减5元)、优惠A+优惠C(共减4元)、 * 优惠B+优惠C(共减3元)、优惠A(共减3元)、优惠B(共减2元)、优惠C(共减1元),用户可以选择优惠组合并进行切换, * 以上例子输入ABC,返回AB、AC、BC、A、B、C */ public class CouponTest { private static List<List<Coupon>> couponCombinationStrategy = new ArrayList<>(); //回溯算法的递归路径 private static LinkedList<Coupon> trackCouponCombination = new LinkedList<>(); public static void main(String[] args) { Coupon a = new Coupon("A", 3); Coupon b = new Coupon("B", 2); Coupon c = new Coupon("C", 1); List<Coupon> couponList = new ArrayList<>(); couponList.add(a); couponList.add(b); couponList.add(c); calculateCouponCombinationStrategy(couponList, 0); for (List<Coupon> coupons : couponCombinationStrategy) { for (Coupon coupon : coupons) { System.out.print(coupon); } System.out.println(); } } private static void calculateCouponCombinationStrategy(List<Coupon> couponList, int start) { if (trackCouponCombination.size() != couponList.size()) { couponCombinationStrategy.add(new ArrayList<>(trackCouponCombination)); } for (int i = start; i < couponList.size(); i++) { trackCouponCombination.add(couponList.get(i)); calculateCouponCombinationStrategy(couponList, i + 1); trackCouponCombination.removeLast(); } } } class Coupon { private String code; private int amount; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } public Coupon(String code, int amount) { this.code = code; this.amount = amount; } @Override public String toString() { return "Coupon{" + "code='" + code + '\'' + ", amount=" + amount + '}'; } }
标签:叠加,code,Coupon,优惠,amount,单元,couponList,共减 From: https://www.cnblogs.com/zhengbiyu/p/18212287