使用数组和求和元素

时间:2015-01-13 11:51:20

标签: java arrays

我必须解决以下问题:给定一个整数数组并给出一个整数值,列出所有可能的数字,形成总和达到给定值的数组。

示例:

Input: array = {1, 2, 2, 3, 4, 5}, int N = 5
Output: {1, 2, 2}, {1, 4}, {5} {2, 3}.

这是我的代码到现在为止,有人可以帮助我吗?

import java.util.Scanner;

public class sumarray {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);
        int[] array = new int[3];
        for (int i = 0; i < array.length; i++) {
            array[i] = scan.nextInt();

        }

        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length; j++) {
                if (array[i] + array[j] == 5) {
                    System.out.println("{" + array[i] + "," + array[j] + "}");
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

这是一个常见的Dynamic Programming问题Subset Sum

如果您只想打印,可以这样做(请注意,您有{2, 3}两次,因为有两个2 s):

public class Main {

    public static void main(String[] args){
        int[] arr = {1, 2, 2, 3, 4, 5};
        subsetSum(arr, 5);
    }

    private static void subsetSum(int[] arr, int sum) {
        subsetSum(arr, 0, sum, "");
    }

    private static String lineSep = System.getProperty("line.separator");
    private static void subsetSum(int[] arr, int i, int sum, String aggregated) {
        if (sum == 0){
            System.out.println("Success with:" + aggregated);
            System.out.println("And done.");
            System.out.println();
            return;
        }
        if (arr.length <= i){
//             failed (uncomment following lines to see why)
//             System.out.println("Failed with:" + aggregated);
//             System.out.println();
            return;
        }

        int current = arr[i];

        subsetSum(arr, i+1, sum, aggregated + lineSep + "not " + current);
        subsetSum(arr, i+1, sum - current, aggregated + lineSep + current);
        return;
    }
}

这使用String是不可变的事实(因此为每个帧创建一个新字符串),并转发所选数字的聚合。我添加了一些文字以使其具有描述性,因此您可以看到正在发生的事情。

输出:

not 1
not 2
not 2
not 3
not 4
5
And done.

not 1
not 2
2
3
And done.

not 1
2
not 2
3
And done.

1
not 2
not 2
not 3
4
And done.

1
2
2
And done.

答案 1 :(得分:0)

这是一个名为Subset Sum的问题。

这是我的代码,我知道不专业,但它是这个特定问题的解决方案

     public static void main(String[] args) {
   Scanner scan = new Scanner(System.in);
    int[] array = new int[6];
    for (int i = 0; i < array.length; i++) {
        array[i] = scan.nextInt();

    }
    int N=5; //N= the given value
    int a1=0; // for use to sure not duplicate answer in this case is {2,3}
    int a2=0; // for use to sure not duplicate answer
    // and we put in a1 and a2 numbers if we add together dose not equal N which is here equal(5)

for (int i = 0; i < array.length; i++) {
    if (array[i]==N)
        System.out.println("{" + array[i] +"}");

  for (int j = i+1; j < array.length; j++) {

     for (int f = j+1; f < array.length; f++){


       if(array[j]+array[i] == 5 ){
           if(a1!=array[i] && a2!=array[j]){  // make sure dose not same number that is done.

                System.out.println("{" + array[i] + ","+  array[j] + "}");
                   a1=array[i];
                   a2=array[j];}}


                else if(array[i] + array[j]+array[f] == 5){
                    System.out.println("{" + array[i] + "," + array[j] + ","+  array[f] + "}");}


        }}}}

输出是:

{1,2,2} {1,4} {2,3} {5}

我希望这可以帮助你:)