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")); } }}
结果一致。
- JAVA函数实现任意给定一组数, 找出任意数相加等于某数或者在一个范围
- 用JAVA写一个函数,功能如下: 任意给定一组数, 找出任意数相加之后的结果为35(任意设定)的情况
- excel 中任意几个数相加后等于一个数
- 给定一段正整数范围,统计9(或者1~9任意一个数)的个数
- Java算法给定一个整数数组,找出其中两个数相加等于目标值
- Java算法给定一个整数数组,找出其中两个数相加等于目标值
- JAVA穷举法递归实现:查找一个数等于一组数中哪些数相加的和
- JAVA穷举法递归实现:查找一个数等于一组数中哪些数相加的和
- leetcode-java.T001_TwoSum 给定一个整数数组,找出其中两个数满足相加等于你指定的目标数字
- N SUM 数组中任意数相加的结果等于剩下的数相加和
- 找出一个有序数组中任意2数之和等于给出的第三个数
- leetcode 1: 找出两个数相加等于给定数 two sum
- [算法]给定一个整型数组,找出能相加起来等于一个特定目标数字的两个数。
- Java求任意一组数正数负数平均数的个数
- 汇编语言实现两个任意大的数相加
- 一个含n个元素的整数数组至少存在一个重复数, 请编程实现,在O(n)时间内找出其中任意一个重复数。
- 给定任意一个整数,找出比当前这个数大的最小的10个素数之和。
- java实现二分法查找任意数
- ACM暑假训练codeforces A. Arcade Game D. Frozen Rivers(康托展开式,spfa)
- n后问题————回溯法求解(递归与非递归)
- mysql replace into 遇上主键自增
- use python to sendmail
- nutch2二次开发笔记
- JAVA函数实现任意给定一组数, 找出任意数相加等于某数或者在一个范围
- 正则中出现多个贪婪模式
- 外观模式 | Facade Pattern
- 设计模式之单例模式
- QNX system architecture 11
- 关于HTTP协议,一篇就够了
- 剑指offer
- 微信小程序 从服务器获取数据(十四)
- QNX system architecture 16