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

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

java-查找其和为给定数的整数序列,java,algorithm,Java,Algorithm,对于n

对于n<10。考4分。我需要打印整数序列,使之和为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;
        }
    }
}
我得到的输出:

    1 1 1 1
这实际上是代码的第一步。稍后我需要打印

    2 1 1
    2 2

所以基本上我的目标是在迭代的每一步得到等于4的和(在本例中)

有一个模式,我会用它来解决这个问题

请注意:

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
把这个模式放在一起,我们只使用两个不同的变量i和j,所以我们可能只需要两个for循环。(就像你一样。)

int exampleNumber=8;
对于(int j=0;j
接下来,我们知道我们从一个长度为8-j的数组开始,然后在i增加时收缩它

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
    }
}
int exampleNumber=8;
对于(int j=0;j
现在我们只需要设置两个重要的数字。开头的那个和旁边的那个

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
    }
}
int exampleNumber=8;
对于(int j=0;j
最后,我们只需要打印我们所拥有的。我们在数组为空的位置替换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();
}
int exampleNumber=8;
对于(int j=0;j
这个不太复杂的解决方案怎么样:

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);
    }

}
公共静态void allSequencesSumToN(int n){
生成(n,“,n);
}
公共静态void generate(int-no、String-str、int-orig){
如果(否==0){
系统输出打印项次(str);
返回;
}
如果(否<0){
返回;
}
对于(int i=1;i
它可能与您需要的略有不同,但您可以调整结果。例如,输入4将产生以下输出:

1111 112 121 13 211 22 三十一


所有可能的序列,但通过简单的后处理,您可以准确地实现所需的结果。

对于所需的输出,是否需要按特定顺序进行?如第一个示例:是否可以接受
2 1 1
而不是
1 2 1
?是的,其顺序应与问题中给出的顺序相同。定义“整数序列”。它是某个给定序列的子序列吗?它是一组小于给定数的整数吗?其中一个序列的数学短语是“划分”。所以你想列出
n
的所有分区。不,模式是这样的1 1 1 1 1 1 2 1 1 1 1 1 1 1 3 1 1 1 1 1 1 4 1 1 1 1 1 1 1 5 1 1 1 6 1 7这基本上是我试图处理的第一次迭代,当循环递增时,下一个模式将是2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 12 4 1 2 5 1 2 6等等。。。直到我们得到8,作为唯一的设置,你可能也想用该模式编辑你的初始帖子。14 11 15 11 16 11 7这基本上是我试图处理的第一次迭代,当循环递增时,下一个模式将是21 11 11 12 11 11 11 11 14 11 15 12 6等等。。。直到我们得到8,因为整数的唯一集合序列是指各个循环中每次迭代的子集。
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();
}
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);
    }

}