将Python分区生成器转换为Java
我试图将以下分区生成器从Python转换为Java,但没有得到预期的结果: Python:将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
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] );