双11购物优惠劵 满减计算程序

改题目为作者购物时突发奇想,问题合理性与代码可行性仍需验证,有相同代码经历的可以私聊我

实现目的:在拥有多个不可叠加的满减优惠劵时,所购物品单价为{a1,a2,a3,a4,a5.}得出最省钱的一套方案,即任意组合数之和最接近满减优惠数额.
{a1,a2,a4}
{a3,a5}

案例输入:

1,99.5,,57,,50.7 

案例输出

{1,99.5}(和为100.5)
{57,50.7}(和为107.7)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class CaseTestPrice {

    /**
     * 用递归的思想来求排列跟组合  然后应用到满减活动
     */
    public static void main(String[] args) {

        // 一堆书的价格
        Object[] tmp = { 13.5,99.5,18.8,8.5,47,9.9,50.7 };
        // Object[] tmp = str.split(" ");
        int minCount = 200;// 满200
        int maxCount = 220;// 自己认为的最大限制
        int num = 0;
        ArrayList<Object[]> rs = cmn(tmp,2);
        // ArrayList<Object[]> rs = cmn(tmp, 3);//3个的组合
        // ArrayList<Object[]> rs = cmn(tmp, 4);//4个的组合
        for (int i = 0; i < rs.size(); i++)

        {
            // System.out.print(i+"=");
            Double countTemp = 0.0;
            String strTemp = "";
            for (int j = 0; j < rs.get(i).length; j++) {
                countTemp = countTemp + Double.parseDouble(rs.get(i)[j].toString());
                // System.out.print(rs.get(i)[j] + ",");
                strTemp += rs.get(i)[j] + ",";
            }
            // System.out.println();
            if (minCount < countTemp && countTemp <= maxCount) {
                num++;
                System.out.println("满" + minCount + "减,方案" + num + ":" + countTemp + "\n组合是:" + strTemp + "\n");
            }

        }

    }

    // 求一个数组的任意组合
    static ArrayList<Object[]> RandomC(Object[] source) {
        ArrayList<Object[]> result = new ArrayList<Object[]>();
        if (source.length == 1) {
            result.add(source);
        } else {
            Object[] psource = new Object[source.length - 1];
            for (int i = 0; i < psource.length; i++) {
                psource[i] = source[i];
            }
            result = RandomC(psource);
            int len = result.size();// fn组合的长度
            result.add((new Object[] { source[source.length - 1] }));
            for (int i = 0; i < len; i++) {
                Object[] tmp = new Object[result.get(i).length + 1];
                for (int j = 0; j < tmp.length - 1; j++) {
                    tmp[j] = result.get(i)[j];
                }
                tmp[tmp.length - 1] = source[source.length - 1];
                result.add(tmp);
            }

        }
        return result;
    }

    // 求指定长度的数组任意组合
    static ArrayList<Object[]> cmn(Object[] source, int n) {
        ArrayList<Object[]> result = new ArrayList<Object[]>();
        if (n == 1) {
            for (int i = 0; i < source.length; i++) {
                result.add(new Object[] { source[i] });

            }
        } else if (source.length == n) {
            result.add(source);
        } else {
            Object[] psource = new Object[source.length - 1];
            for (int i = 0; i < psource.length; i++) {
                psource[i] = source[i];
            }
            result = cmn(psource, n);
            ArrayList<Object[]> tmp = cmn(psource, n - 1);
            for (int i = 0; i < tmp.size(); i++) {
                Object[] rs = new Object[n];
                for (int j = 0; j < n - 1; j++) {
                    rs[j] = tmp.get(i)[j];
                }
                rs[n - 1] = source[source.length - 1];
                result.add(rs);
            }
        }
        return result;
    }

}```

发布了18 篇原创文章 · 获赞 15 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览