Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在分数背包实现中,我的解决方案总是使用相同的最大索引。如何按降序排列数组?_Java_Algorithm_Sorting_Knapsack Problem_Greedy - Fatal编程技术网

Java 在分数背包实现中,我的解决方案总是使用相同的最大索引。如何按降序排列数组?

Java 在分数背包实现中,我的解决方案总是使用相同的最大索引。如何按降序排列数组?,java,algorithm,sorting,knapsack-problem,greedy,Java,Algorithm,Sorting,Knapsack Problem,Greedy,正确输出: 180.0000 我的输出: 200.0 我的代码总是使用相同的最大索引。我是编程初学者要使其正常工作,您需要更改: 更改权重[i]=(权重[max_索引]-b)到权重[max_index]=(权重[max_index]-b)因为您正在修改最大索引权重,而不是另一项的权重(索引错误) 初始化索引中的选择最大索引方法(从索引=0到索引=-1)。这不是必需的,但如果(max_index>=0)是有意义的,那就好了 您的算法的运行时间是O(n2)。最好开始代码,根据分数背包标准对所有项目进

正确输出:

180.0000

我的输出:

200.0


我的代码总是使用相同的最大索引。我是编程初学者

要使其正常工作,您需要更改:

  • 更改
    权重[i]=(权重[max_索引]-b)
    权重[max_index]=(权重[max_index]-b)因为您正在修改
    最大索引
    权重,而不是另一项的权重(索引错误
  • 初始化
    索引
    中的
    选择最大索引
    方法(从
    索引=0
    索引=-1
    )。这不是必需的,但如果(max_index>=0)
    是有意义的,那就好了
  • 您的算法的运行时间是O(n2)。最好开始代码,根据分数背包标准对所有项目进行排序(首先是更大的
    值/重量
    )。你需要使用一种新的方法。通过按这样的顺序处理项目,时间复杂度将是O(n logn),这要好得多

    import java.util.Scanner;
    import java.math.RoundingMode;
    import java.text.DecimalFormat;
    
    import static java.lang.Integer.min;
    
    public class FractionalKnapsack {
      // this method for calculating the maximum index
     
      public static int select_max_index(int []values,int []weights,int n){
    
            int index=0;
            double max=0;
    
            for(int i=0;i<n;i++) {
    
                if (weights[i] > 0 && (double) values[i] / (double) weights[i] > max) {
    
                    max = (double) values[i] / (double) weights[i];
                    index = i;
                }
            }
    
            return index;
        }
    
        private static double getOptimalValue(int  capacity, int  [] values, int[] weights,int n) {
    
            // fractional knapsack problem
            int i;
            int max_index=0;
            double value = 0.0000d;
            if (capacity == 0)
                return value;
            for (i = 0; i < n; i++) {
                max_index = select_max_index(values,weights,n);// call the maximum index
                if (max_index >= 0) {
                    int b = min(capacity, (weights[max_index]));
                    value = value + b * ((double) values[max_index] / (double) weights[max_index]);
                    weights[i] = (weights[max_index] - b);
                    capacity = capacity - b;
                }
            }
    
            return value;
        }
    
    
        public static void main(String args[]) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            int capacity = scanner.nextInt();
            int[] values = new int[n+2];
            int[] weights = new int[n+2];
            for (int i = 0; i < n; i++) {
                values[i] = scanner.nextInt();
                weights[i] = scanner.nextInt();
            }
            DecimalFormat df = new DecimalFormat(".0000"); // for getting the decimal point upto 4 digits
            df.setRoundingMode(RoundingMode.CEILING);
            System.out.println(df.format(getOptimalValue(capacity, values, weights,n)));
        }
    }
    
    - 3 50
    60 20
    100 50
    120 30