gpt4 book ai didi

java - 将代码从 python 转换为 Java

转载 作者:行者123 更新时间:2023-12-01 21:08:23 25 4
gpt4 key购买 nike

我正在尝试转换this从 python 到 java 的代码,我陷入困境并需要帮助:

代码找到数组中最长的连续体,连续体中的值的总和等于零模3,例如对于数组a[]={2,-3,5,7,-20 ,7}

我们有 2-3+5+7-20=-9,所以输出是 5,

Python 代码:

a = [2,-3,5,7,-20,7]

best = 0
bestIndex = -1

dp = [[0,0,0] for i in range(len(a) + 1)]

for i in range(1,len(a) + 1):
r = a[i-1] % 3

for j in range(3):
canSumToJ = dp[i-1][(3 + j - r) % 3] > 0

dp[i][j] = max(1 if r == j else 0
,1 + dp[i-1][(3 + j - r) % 3] if canSumToJ else 0)

bestIndex = i - 1 if dp[i][0] > best else bestIndex
best = max(best,dp[i][0])

print(best,(bestIndex - best + 1, bestIndex)) # (5, (0, 4))

# dp
# => [[0, 0, 0]
# ,[0, 0, 1]
# ,[1, 0, 2]
# ,[0, 3, 2]
# ,[3, 1, 4]
# ,[5, 4, 2]
# ,[3, 6, 5]]

尝试转换为 Java:

 public class mmn123 {
public static void main(String[] args)
{
//int[] a = {1, 3, 3, 3, 3, 1};
int[] a = {2, -3, 5, 7, -20, 7};
int best = 0;
//int bestIndex=-1 ;
int len=a.length;
int r;
int a1[]={0,0,0};
int a2[]={0,0,0};
int a3[]={0,0,0};
int a4[]={0,0,0};
int a5[]={0,0,0};
int a6[]={0,0,0};
int a7[]={0,0,0};
boolean canSumToJ=false;
int [][]dp={a1,a2,a3,a4,a5,a6,a7};



// for i in range(1,len(a) + 1):
for(int i=1;i<len+1;i++)
{
r = a[i-1] % 3;

//for j in range(3):
for(int j=0;j<3;j++)
{
if(dp[i][(3 + j - r) % 3]>0)
canSumToJ =true; ;

if(r==j&&canSumToJ ==true)
dp[i][j] = Math.max(1,1 + dp[i-1][(3 + j - r) % 3]);
if(r!=j&&canSumToJ ==true)
dp[i][j] = Math.max(0,1 + dp[i-1][(3 + j - r) % 3]);
if(r!=j&&canSumToJ ==false)
dp[i][j] = Math.max(0,0);
if(r==j&&canSumToJ ==false)
dp[i][j] = Math.max(1,0);
canSumToJ=false;
}
//canSumToJ=false;
//if (dp[i][0] > best)
//bestIndex = (i - 1);
//else bestIndex;
best = Math.max(best,dp[i][0]);
}
for(int k=0;k<a.length;k++)
{
for(int l=0;l<3;l++)
{
System.out.print(dp[k][l]);
}
System.out.println();
}
System.out.println("best: "+best);
/*
# dp
# => [[0, 0, 0]
# ,[0, 0, 1]
# ,[1, 0, 2]
# ,[0, 3, 2]
# ,[3, 1, 4]
# ,[5, 4, 2]
# ,[3, 6, 5]]
*/
}
}

The outputs are not the same.

最佳答案

有两个问题:

第一:拼写错误 - 您忘记了 dp[i-1] 中的 -1

    if(dp[i-1][(3 + j - r) % 3]>0)
canSumToJ =true; ;

顺便说一句:你可以像Python一样编写它

    canSumToJ = (dp[i-1][(3 + j - r) % 3] > 0)

第二: Java 中的模给出负值 - 所以你得到 -20 % 3 = -2

所以:Mod in Java produces negative numbers

您可以编写自己的函数来计算正模。

public class Test {

public static int modulo(int a, int b)
{
int r = a % b;

if(r < 0)
{
//System.out.println(a + " % " + b + " = " + r);
return r + b;
}

return r;
}

public static void main(String[] args)
{
int[] a = {2, -3, 5, 7, -20, 7};
int[][] dp={{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}};

int r;
int best = 0;

boolean canSumToJ = false;

int m, x, y;

for(int i = 1 ; i < a.length + 1 ; i++)
{
r = modulo(a[i-1], 3);

for(int j = 0 ; j < 3 ; j++)
{
m = modulo(3+j-r, 3);

canSumToJ = (dp[i-1][m] > 0);

if(r == j)
x = 1;
else
x = 0;

if(canSumToJ == true)
y = 1 + dp[i-1][m];
else
y = 0;

dp[i][j] = Math.max(x, y);
}

best = Math.max(best, dp[i][0]);
}

System.out.println();

for(int k = 0 ; k < a.length ; k++)
{
for(int l = 0 ; l < 3 ; l++)
{
System.out.print(dp[k][l]);
}
System.out.println();
}

System.out.println("best: " + best);

/*
# dp
# => [[0, 0, 0]
# ,[0, 0, 1]
# ,[1, 0, 2]
# ,[0, 3, 2]
# ,[3, 1, 4]
# ,[5, 4, 2]
# ,[3, 6, 5]]
*/
}
}

关于java - 将代码从 python 转换为 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41883585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com