JAVA函数实现任意给定一组数, 找出任意数相加等于某数或者在一个范围

来源:互联网 发布:数据库基础知识总结 编辑:程序博客网 时间:2024/06/17 08:47

该函数是在项目偶然使用写出来的,使用在给出一组商品的价格,需要找出这组商品所有的组合情况,方法一是所有结果为一个固定值,方法二是一个范围。

方法一:

public class Test {    public static void main(String[] args) {        String str = "36,60,50,15,35,17,18,44,72,12";        int sum = 100;        diguiSum(str, sum);    }    public static void diguiSum(String str, int sum) {        String[] x = str.split(",");        int[] array = arrayTransform(x);        for (int i = 0; i < 10; i++) {            int[] cache = new int[i + 1];            int ceng = -1;            int cengQuit = i;            int startPiont = 0;            cir(ceng, cengQuit, startPiont, array, cache, sum);        }    }    // 递归求结果    public static void cir(int ceng, int cengQuit, int startPiont, int[] array, int[] cache, int sum) {        ceng++;        for (int i = startPiont; i < array.length; i++) {            cache[ceng] = array[i];            if (ceng == cengQuit) {                if (getSum(cache) == sum) {                    printcache(cache);                }                if (getSum(cache) > sum) {                    break;                }            }            if (ceng < cengQuit) {                startPiont = i;                cir(ceng, cengQuit, startPiont, array, cache, sum);            }        }    }    // 获取组合数字之和    public static int getSum(int[] cache) {        int sum = 0;        for (int i = 0; i < cache.length; i++) {            sum = sum + cache[i];        }        return sum;    }    // 打印组合的可能    public static void printcache(int[] cache) {        for (int i = 0; i < cache.length; i++) {            System.out.print(cache[i] + ",");        }        System.out.println();    }    // 转换数组类型 且为提高效率做准备    public static int[] arrayTransform(String[] strArray) {        int length = 0;        int[] array = new int[strArray.length];        for (int i = 0; i < strArray.length; i++) {            array[i] = Integer.valueOf(strArray[i]);        }        Arrays.sort(array);        for (int i = 0; i < array.length; i++) {            if (array[i] > 100) {                length = i;                break;            }        }        int[] dest = new int[length];        if(length == 0){            return array;        }        System.arraycopy(array, 0, dest, 0, length);        return dest;    }}

输出:

50,50,
12,44,44,
15,35,50,
12,17,35,36,
12,18,35,35,
15,15,35,35,
15,17,18,50,
12,12,15,17,44,
12,17,17,18,36,
12,17,18,18,35,
15,15,17,17,36,
15,15,17,18,35,
12,12,12,12,17,35,
12,17,17,18,18,18,
15,15,17,17,18,18,
15,17,17,17,17,17,
12,12,12,12,17,17,18,
12,12,12,15,15,17,17,


方法二:

public class Test {    public static void main(String[] args) {        List<Double> str1 = new ArrayList<>();        str1.add(20.21);        str1.add(99.25);        str1.add(120.0);        str1.add(74.05);        str1.add(34.5);        str1.add(50.00);        Double min = 100.00;        Double max = 150.00;        diguiSum(arrayTransform(str1,max),min,max);    }    public static void diguiSum(List<Double> str,Double min,Double max) {        for (int i = 0; i < 50; i++) {            //System.out.println(str.get(i));            Double[] cache = new Double[i + 1];            int ceng = -1;            int cengQuit = i;            int startPiont = 0;            cir(ceng, cengQuit, startPiont, str, cache, min,max);        }    }    // 递归求结果    public static void cir(int ceng, int cengQuit, int startPiont, List<Double> array, Double[] cache, Double min,Double max) {        ceng++;        for (int i = startPiont; i < array.size(); i++) {            cache[ceng] = array.get(i);            if (ceng == cengQuit) {                if ((getSum(cache) >= min) && (getSum(cache) <= max)) {                    printcache(cache);                }                if ((getSum(cache) < min) || (getSum(cache) > max)) {                    continue;                }            }            if (ceng < cengQuit) {                startPiont = i;                cir(ceng, cengQuit, startPiont, array, cache,min,max);            }        }    }    // 获取组合数字之和    public static Double getSum(Double[] cache) {        Double sum = 0.00;        for (int i = 0; i < cache.length; i++) {            sum = sum + cache[i];        }        return sum;    }    // 打印组合的可能    public static void printcache(Double[] cache) {        for (int i = 0; i < cache.length; i++) {            System.out.print(cache[i] + ",");        }        System.out.println();    }    public static List<Double> arrayTransform(List<Double> strArray,Double max) {        for (int i = 0; i < strArray.size() - 1; i++) {            for (int j = 1; j < strArray.size() - i; j++) {                Double a;                if ((strArray.get(j - 1)).compareTo(strArray.get(j)) > 0) { // 比较两个整数的大小                    a = strArray.get(j - 1);                    strArray.set((j - 1), strArray.get(j));                    strArray.set(j, a);                }            }        }        for (int i = 0; i < strArray.size(); i++) {            if (strArray.get(i) > max) {                strArray.remove(i);            }        }        return strArray;    }}

输出:

120.0,
20.21,99.25,
20.21,120.0,
34.5,74.05,
34.5,99.25,
50.0,50.0,
50.0,74.05,
50.0,99.25,
74.05,74.05,
20.21,20.21,74.05,
20.21,20.21,99.25,
20.21,34.5,50.0,
20.21,34.5,74.05,
20.21,50.0,50.0,
20.21,50.0,74.05,
34.5,34.5,34.5,
34.5,34.5,50.0,
34.5,34.5,74.05,
34.5,50.0,50.0,
50.0,50.0,50.0,
20.21,20.21,20.21,50.0,
20.21,20.21,20.21,74.05,
20.21,20.21,34.5,34.5,
20.21,20.21,34.5,50.0,
20.21,20.21,34.5,74.05,
20.21,20.21,50.0,50.0,
20.21,34.5,34.5,34.5,
20.21,34.5,34.5,50.0,
34.5,34.5,34.5,34.5,
20.21,20.21,20.21,20.21,20.21,
20.21,20.21,20.21,20.21,34.5,
20.21,20.21,20.21,20.21,50.0,
20.21,20.21,20.21,34.5,34.5,
20.21,20.21,20.21,34.5,50.0,
20.21,20.21,34.5,34.5,34.5,
20.21,20.21,20.21,20.21,20.21,20.21,
20.21,20.21,20.21,20.21,20.21,34.5,
20.21,20.21,20.21,20.21,34.5,34.5,
20.21,20.21,20.21,20.21,20.21,20.21,20.21,

方法三,封装成传入list<Double>的工具类,便于项目使用:

public class ProductPriceGroupUtil {    public static List<Map<String,Object>> getProductPriceGroup(List<Double> str1, Double min, Double max) {        List<Map<String,Object>> pds = new ArrayList<>();        diguiSum(arrayTransform(str1,max),min,max,pds);        return pds;    }    public static void diguiSum(List<Double> str, Double min, Double max,List<Map<String,Object>> pds) {        for (int i = 0; i < 10; i++) {            Double[] cache = new Double[i + 1];            int ceng = -1;            int cengQuit = i;            int startPiont = 0;            cir(ceng, cengQuit, startPiont, str, cache, min,max,pds);        }    }    // 递归求结果    public static void cir(int ceng, int cengQuit, int startPiont, List<Double> array, Double[] cache, Double min,Double max,List<Map<String,Object>> pds) {        ceng++;        for (int i = startPiont; i < array.size(); i++) {            cache[ceng] = array.get(i);            if (ceng == cengQuit) {                if ((getSum(cache) >= min) && (getSum(cache) <= max)) {                    pds.add(printcache(cache));                }                if ((getSum(cache) < min) || (getSum(cache) > max)) {                    continue;                }            }            if (ceng < cengQuit) {                startPiont = i;                cir(ceng, cengQuit, startPiont, array, cache,min,max,pds);            }        }    }    // 获取组合数字之和    public static Double getSum(Double[] cache) {        Double sum = 0.00;        for (int i = 0; i < cache.length; i++) {            sum = sum + cache[i];        }        return sum;    }    // 打印组合的可能    public static Map<String,Object> printcache(Double[] cache) {        Map<String,Object> pd = new HashedMap();        Double sum = 0.00;        String s = "";        for (int i = 0; i < cache.length; i++) {            if(i != cache.length - 1){                s += (cache[i] + ",");                sum += cache[i];            }else{                s += cache[i];                sum += cache[i];            }        }        pd.put("price_sum",sum);        pd.put("price_group",s);        pd.put("group_size",cache.length);        return pd;    }    public static List<Double> arrayTransform(List<Double> strArray,Double max) {        for (int i = 0; i < strArray.size() - 1; i++) {            for (int j = 1; j < strArray.size() - i; j++) {                Double a;                if ((strArray.get(j - 1)).compareTo(strArray.get(j)) > 0) { // 比较两个整数的大小                    a = strArray.get(j - 1);                    strArray.set((j - 1), strArray.get(j));                    strArray.set(j, a);                }            }        }        for (int i = 0; i < strArray.size(); i++) {            if (strArray.get(i) > max) {                strArray.remove(i);            }        }        return strArray;    }}

调用测试:

public class Test {    public static void main(String[] args) {        List<Double> str1 = new ArrayList<>();        str1.add(20.21);        str1.add(99.25);        str1.add(120.0);        str1.add(74.05);        str1.add(34.5);        str1.add(50.00);        Double min = 100.00;        Double max = 150.00;        List<Map<String,Object>> result = ProductPriceGroupUtil.getProductPriceGroup(str1,min,max);        for(Map<String,Object> map : result){            System.out.println(map.get("price_group"));        }    }}

结果一致。

阅读全文
0 0
原创粉丝点击