Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.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 - Fatal编程技术网

Java 总计到给定数量的子集计数(允许重复)。没有得到正确的输出

Java 总计到给定数量的子集计数(允许重复)。没有得到正确的输出,java,Java,这个代码有什么问题 它类似于子集和问题。这里唯一的区别是我们有无限的数组元素。 我的输出: 7.1 7.1 5.3 5.3 5.1 5.3 5.3 5.1 3 3 1 3 3 1 3 1 3 1 1 13 我正在打印代码中包含的所有组合,以供参考。有些组合打印两次或三次。我应该做哪些更改来跳过重复的组合 Output: 6 import java.util.*; 公共阶级面额{ 公共静态int结果=0; 公共静态无效计数(int n,int[]arr,int sum,Stack out){ i

这个代码有什么问题

它类似于子集和问题。这里唯一的区别是我们有无限的数组元素。 我的输出:
7.1
7.1
5.3
5.3
5.1
5.3
5.3
5.1
3 3 1
3 3 1
3 1
3 1
1
13
我正在打印代码中包含的所有组合,以供参考。有些组合打印两次或三次。我应该做哪些更改来跳过重复的组合

Output: 6
import java.util.*;
公共阶级面额{
公共静态int结果=0;
公共静态无效计数(int n,int[]arr,int sum,Stack out){

if(sum注意
1,7
7,1
都是和8相同的子集

我们可以将子集表示为
映射
,其中:

  • 键-来自
    arr
    的元素
  • 值-使用的计数
使用此表示法,
1,7
7,1
都将表示为
映射
=
{1:1,7:1}
(键在
映射
中不排序)

我们可以在
集中存储唯一的子集

现在,编写代码非常简单:

import java.util.*;
public class denomination {
    public static int result=0;
    public static void count(int n, int[] arr, int sum, Stack<Integer> out){
        if(sum<0 || n<=0){
            return;
        }
        if(sum==0){
            result++;
            for (int x : out)
                System.out.print(x + " ");
            System.out.println();
            return;
        }
        out.push(arr[n-1]);
        count(n, arr, sum-arr[n-1], out);
        count(n-1, arr, sum-arr[n-1],out);
        if(!out.empty())
            out.pop();
        count(n-1, arr, sum, out);
    }
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int arr[] = new int[n];
        for(int i=0;i<n;i++)
            arr[i] = in.nextInt();
        int sum = in.nextInt();
        Stack<Integer> out = new Stack<Integer>();
        count(n, arr, sum, out);
        System.out.println(result);
    }
}

非常感谢……成功了:)
import java.util.*;
public class denomination {
    public static int result=0;
    public static void count(int n, int[] arr, int sum, Stack<Integer> out){
        if(sum<0 || n<=0){
            return;
        }
        if(sum==0){
            result++;
            for (int x : out)
                System.out.print(x + " ");
            System.out.println();
            return;
        }
        out.push(arr[n-1]);
        count(n, arr, sum-arr[n-1], out);
        count(n-1, arr, sum-arr[n-1],out);
        if(!out.empty())
            out.pop();
        count(n-1, arr, sum, out);
    }
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int arr[] = new int[n];
        for(int i=0;i<n;i++)
            arr[i] = in.nextInt();
        int sum = in.nextInt();
        Stack<Integer> out = new Stack<Integer>();
        count(n, arr, sum, out);
        System.out.println(result);
    }
}
public class Demo {
    public static void count(int[] arr, int targetSum, Map<Integer, Integer> currMap, Set<Map<Integer, Integer>> subsets) {
        if (targetSum > 0) { 
            for (int integer : arr) {
                Map<Integer, Integer> newMap = new HashMap<>(currMap);
                Integer integerUseCount = currMap.getOrDefault(integer, 0);
                newMap.put(integer, integerUseCount + 1);

                count(arr, targetSum - integer, newMap, subsets); // "Let's try with this"
            }
        } else if (targetSum == 0) { // We found a subset
            subsets.add(currMap);
        }
    }

    public static void main(String[] args) {
        Set<Map<Integer, Integer>> subsets = new HashSet<>();
        count(new int[]{1, 3, 5, 7}, 8, new HashMap<>(), subsets);
        System.out.println("Output: "+ subsets.size());
        System.out.println("Subsets are:");
        subsets.forEach(System.out::println);
    }
}
Output: 6
Subsets are:
{1=2, 3=2}
{1=5, 3=1}
{1=3, 5=1}
{1=1, 7=1}
{5=1, 3=1}
{1=8}