简体   繁体   English

java-查找总和为给定数字的整数序列

[英]java - find sequence of integers whose sum is that of a given number

For n < 10. taking 4 for test. 对于n <10。取4进行测试。 I need to print sequences of integers such that the sum of them is n. 我需要打印整数序列,以使它们的总和为n。

class Test {
    public static void main(String args[]) {
        printAll(4);
    }

    public static void printAll(int k) {
        int count = 0;
        int a[] = new int[k];
        for (int i = 0; i < k; i++) {
            a[i] = 1;
        }
        int currentSum = 0;
        a[1] = 0;
        for (int i = 0; i < k; i++) {
            a[1]++;
            for (int j = 0; j < k - i; j++) {
                currentSum += a[i];
                count++;
                if (currentSum == k) {
                    for (int m = 0; m < count; m++) {
                        System.out.print(a[m] + " ");
                    }
                    System.out.println();
                }
            }
            count = 0;
        }
    }
}

for n = 4: 对于n = 4:

Desired output: 所需的输出:

    1 1 1 1
    1 2 1 
    1 3

Output I get: 我得到的输出:

    1 1 1 1

This is actually the first step of code. 这实际上是代码的第一步。 Later i need to print 以后我需要打印

    2 1 1
    2 2

So basically my target is to get sum equal to 4(in this case) at each step of iteration 所以基本上我的目标是在迭代的每个步骤中获得等于4的和(在这种情况下)

There is a pattern which I would use to solve this problem. 有一种模式可以用来解决这个问题。

Notice this: 注意:

example: 4 //i starts at 0 and increments 1
1 1 1 1 = 1 1+i 1 1  //4-i numbers
1 2 1   = 1 1+i 1    //4-i numbers
1 3     = 1 1+i      //4-i numbers

example: 8
1 1 1 1 1 1 1 1 = 1 1+i 1 1 1 1 1 1 //8-i numbers
1 2 1 1 1 1 1   = 1 1+i 1 1 1 1 1   //8-i numbers
1 3 1 1 1 1     = 1 1+i 1 1 1 1     //8-i numbers
1 4 1 1 1       //and so on...
1 5 1 1
1 6 1
1 7

//Now we introduce a new level, with 'j' (j was 0 in previous example, now it is 1)
//We add i to j in the second position in the array
2 1 1 1 1 1 1 = 1+j j+i 1 1 1 1 //8-j-i numbers
2 2 1 1 1 1   = 1+j j+i 1 1 1   //8-j-i numbers
2 3 1 1 1     = 1+j j+i 1 1     //8-j-i numbers
2 4 1 1       = 1+j j+i 1       //8-j-i numbers
2 5 1
2 6

Putting this pattern together, we only use two different variables i and j, so we probably only need two for loops. 将此模式放在一起,我们仅使用两个不同的变量i和j,因此我们可能仅需要两个for循环。 (like you have.) (就像你一样。)

int exampleNumber = 8;
for (int j = 0; j < exampleNumber; j++)
{
    for (int i = 0; i < exampleNumber; i++)
    {

    }
}

Next, we know we start with an array of 8-j length, and then shrink it when i increments as well. 接下来,我们知道我们从一个长度为8-j的数组开始,然后在我递增时缩小它。

int exampleNumber = 8;
for (int j = 0; j < exampleNumber; j++)
{
    int jLength = exampleNumber - j; //NewLine
    for (int i = 0; i < exampleNumber; i++)
    {
        int iArray = new int[jLength - i]; //NewLine
    }
}

Now we just have to set the two important numbers. 现在我们只需要设置两个重要的数字。 The one at the start and the one next to it. 一开始是一个,旁边是一个。

int exampleNumber = 8;
for (int j = 0; j < exampleNumber; j++)
{
    int jLength = exampleNumber - j;
    for (int i = 0; i < exampleNumber; i++)
    {
        int iArray = new int[jLength - i];
        iArray[0] = 1 + j; //NewLine
        iArray[1] = j + i; //NewLine
    }
}

Finally, we just need to print what we have. 最后,我们只需要打印出我们所拥有的。 We substitue 1's where the array is null. 我们在数组为null的地方替换1。

int exampleNumber = 8;
for (int j = 0; j < exampleNumber; j++)
{
    int jLength = exampleNumber - j;
    for (int i = 0; i < exampleNumber; i++)
    {
        int iArray = new int[jLength - i];
        iArray[0] = 1 + j;
        iArray[1] = j + i;
        //printIntArray(iArray);
    }
}

public void printIntArray(int[] printArray)
{
    for (int i = 0; i < printArray.length(); i++)
    {
        int numberToPrint = printArray[i];
        if (numberToPrint == null || numberToPrint == 0)
            numberToPrint = 1;
        System.out.print(numberToPrint + " ");
    }
    System.out.println();
}

How about this less complicated solution: 这个不太复杂的解决方案怎么样:

public static void allSequencesSumToN(int n) {
    generate(n,"", n);
}

public static void generate(int no, String str, int orig) {
    if(no == 0) {
        System.out.println(str);
        return;
    }
    if(no < 0) {
        return;
    }
    for(int i = 1; i < orig ; i++) {
        waysUtil(no - i, str + i, orig);
    }

}

It might be slightly different than what you need, but you can adjust the result. 它可能与您所需要的稍有不同,但是您可以调整结果。 Eg input 4, this will generate following output: 例如输入4,将生成以下输出:

1111 112 121 13 211 22 31 1111 112 121 13 211 22 31

All possible sequences, but with easy postprocessing you can achieve exactly what you need. 所有可能的序列,但是通过简单的后处理,您都可以完全实现所需的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在整数数组中找到所有对,其和等于给定数字 - How to Find all Pairs in an Array of Integers Whose sum is Equal to a Given Number 给定整数集的子集,其和为常数 N:Java - Subsets of a given Set of Integers whose sum is a Constant N : Java 在 java 中通过 hashmap 从数组中查找所有元素对,其总和等于给定数 - Find all pairs of elements from an array whose sum is equal to given number by hashmap in java 如何找到Java中两个给定整数之间的数字总和 - How to find the sum of the numbers that are between the two given integers in Java 如何在O(n)中的排序数组中找到两个总和为给定数字的数字? - How to find two number whose sum is given number in sorted array in O(n)? 如何找到总和等于或小于给定数量的元组数? - How to find number of tuples whose sum is equal or less than a given number? 给定2d整数数组,找到一个递归地总结给定数字的路径 - Given 2d array of integers, find a path that sum up to a given number recursively 在数组中查找值总和等于给定总和的索引对 - Find index pairs in array whose value sum is equal to given sum 部分和永远不会达到给定值的整数序列的置换 - Permutation of a sequence of integers where partial sum never reaches given values 从总和等于给定数字的数组中计算对? - Count pairs from an array whose sum is equal to a given number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM