From 45a230e870b26b51d3006273a36df78203521253 Mon Sep 17 00:00:00 2001
From: skyouc
Date: 星期一, 28 四月 2025 21:28:48 +0800
Subject: [PATCH] 入库单数据显示异常问题修复

---
 rsf-server/src/main/Test/CombinationFinder.java |  123 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 123 insertions(+), 0 deletions(-)

diff --git a/rsf-server/src/main/Test/CombinationFinder.java b/rsf-server/src/main/Test/CombinationFinder.java
new file mode 100644
index 0000000..cd7ee08
--- /dev/null
+++ b/rsf-server/src/main/Test/CombinationFinder.java
@@ -0,0 +1,123 @@
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public class CombinationFinder {
+
+    public List<Integer> findCombination(double[] nums, double target) {
+        // 澶勭悊绛夊�兼儏鍐�
+        List<Integer> equal = findEqual(nums, target);
+        if (equal != null && !equal.isEmpty()) {
+            return equal;
+        }
+
+        // 澶勭悊澶т簬鐨勬儏鍐�
+        List<Integer> greater = findGreater(nums, target);
+        if (greater != null && !greater.isEmpty()) {
+            return greater;
+        }
+
+        // 澶勭悊灏忎簬鐨勬儏鍐�
+        List<Integer> less = findLess(nums, target);
+        return less != null ? less : new ArrayList<>(); // 纭繚涓嶈繑鍥瀗ull
+    }
+
+    private List<Integer> findEqual(double[] nums, double target) {
+        int n = nums.length;
+        double[] dp = new double[(int) (target * 100 + 1)]; // 鏀惧ぇ100鍊嶅鐞嗙簿搴﹂棶棰�
+        Arrays.fill(dp, Double.MAX_VALUE);
+        dp[0] = 0;
+        int[] prev = new int[(int) (target * 100 + 1)];
+        Arrays.fill(prev, -1);
+
+        for (int j = 0; j < n; j++) {
+            int num = (int) (nums[j] * 100); // 鏀惧ぇ100鍊嶅鐞嗙簿搴﹂棶棰�
+            for (int i = (int) (target * 100); i >= num; i--) {
+                if (dp[i - num] != Double.MAX_VALUE && dp[i - num] + 1 < dp[i]) {
+                    dp[i] = dp[i - num] + 1;
+                    prev[i] = j;
+                }
+            }
+        }
+
+        if (dp[(int) (target * 100)] == Double.MAX_VALUE) {
+            return null;
+        }
+
+        List<Integer> indices = new ArrayList<>();
+        int current = (int) (target * 100);
+        while (current > 0) {
+            int j = prev[current];
+            if (j == -1) return null;
+            indices.add(j);
+            current -= (int) (nums[j] * 100);
+        }
+
+        return indices;
+    }
+
+    private List<Integer> findGreater(double[] nums, double target) {
+        List<double[]> sorted = new ArrayList<>();
+        for (int i = 0; i < nums.length; i++) {
+            sorted.add(new double[]{nums[i], i});
+        }
+        sorted.sort((a, b) -> Double.compare(b[0], a[0]));
+
+        double sum = 0;
+        List<Integer> indices = new ArrayList<>();
+        for (double[] pair : sorted) {
+            sum += pair[0];
+            indices.add((int) pair[1]);
+            if (sum > target) {
+                return indices;
+            }
+        }
+
+        return null;
+    }
+
+    private List<Integer> findLess(double[] nums, double target) {
+        int n = nums.length;
+        List<Integer> bestIndices = new ArrayList<>();
+        double[] maxSum = {-1.0};
+        for (int k = 1; k <= n; k++) {
+            List<Integer> currentIndices = new ArrayList<>();
+            double[] currentMax = {-1.0};
+            backtrack(nums, target, 0, k, 0.0, 0, currentIndices, currentMax, new ArrayList<>());
+            if (currentMax[0] != -1.0) {
+                if (currentMax[0] > maxSum[0] || (currentMax[0] == maxSum[0] && currentIndices.size() < bestIndices.size())) {
+                    maxSum[0] = currentMax[0];
+                    bestIndices = new ArrayList<>(currentIndices);
+                }
+            }
+        }
+
+        return bestIndices.isEmpty() ? null : bestIndices;
+    }
+
+    private void backtrack(double[] nums, double target, int start, int k, double currentSum, int count, List<Integer> currentIndices, double[] currentMax, List<Integer> path) {
+        if (count == k) {
+            if (currentSum <= target && currentSum > currentMax[0]) {
+                currentMax[0] = currentSum;
+                currentIndices.clear();
+                currentIndices.addAll(path);
+            }
+            return;
+        }
+
+        for (int i = start; i < nums.length; i++) {
+            if (currentSum + nums[i] > target) continue;
+            path.add(i);
+            backtrack(nums, target, i + 1, k, currentSum + nums[i], count + 1, currentIndices, currentMax, path);
+            path.remove(path.size() - 1);
+        }
+    }
+
+    public static void main(String[] args) {
+        CombinationFinder finder = new CombinationFinder();
+        double[] nums = {3.0, 1.0, 4.0, 2.0};
+        double target = 0.1;
+        List<Integer> result = finder.findCombination(nums, target);
+        System.out.println("鏈�浼樼粍鍚堢殑绱㈠紩: " + result); // 渚嬪锛岀瓑鍊肩粍鍚堝彲鑳戒负绱㈠紩2锛�4.0锛夊拰3锛�1.0锛�
+    }
+}
\ No newline at end of file

--
Gitblit v1.9.1