Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.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
将Python分区生成器转换为Java_Java_Python_Combinatorics_Code Conversion - Fatal编程技术网

将Python分区生成器转换为Java

将Python分区生成器转换为Java,java,python,combinatorics,code-conversion,Java,Python,Combinatorics,Code Conversion,我试图将以下分区生成器从Python转换为Java,但没有得到预期的结果: Python: def accelAsc(n): a = [0 for i in range(n + 1)] k = 1 a[0] = 0 y = n - 1 while k != 0: x = a[k - 1] + 1 k -= 1 while 2*x <= y: a[k] = x

我试图将以下分区生成器从Python转换为Java,但没有得到预期的结果:

Python:

def accelAsc(n):
    a = [0 for i in range(n + 1)]
    k = 1
    a[0] = 0
    y = n - 1
    while k != 0:
        x = a[k - 1] + 1
        k -= 1
        while 2*x <= y:
            a[k] = x
            y -= x
            k += 1
        l = k + 1
        while x <= y:
            a[k] = x
            a[l] = y
            yield a[:k + 2]
            x += 1
            y -= 1
        a[k] = x + y
        y = x + y - 1
        yield a[:k + 1]

这是不正确的。

凯文的评论引出了答案:

替换

for( int xValue = 0; xValue <= k; xValue++ ) System.out.print( a[xValue + 2] );
for( int xValue = 0; xValue <= k; xValue++ ) System.out.print( a[xValue + 1] );

for(int xValue=0;xValue对于初学者,
a[:k+2]
表示“从[0]到[k+2]的所有项目”,但您的等效System.out.print处于从[2]到[k+2]的for循环中@Kevin谢谢你,问题解决了;我已经更新了问题。你应该回答你自己的问题,然后检查它是否已回答,因为它仍然是开放的。
for( int xValue = 0; xValue <= k; xValue++ ) System.out.print( a[xValue + 2] );
for( int xValue = 0; xValue < k + 2; xValue++ ) System.out.print( a[xValue] );
for( int xValue = 0; xValue <= k; xValue++ ) System.out.print( a[xValue + 1] );
for( int xValue = 0; xValue < k + 1; xValue++ ) System.out.print( a[xValue] );