我目前正在尝试解决一个问题,即尝试找到三角形数组中项的最小三角形和。 (我当前使用的数组是一个3x3的三角形数组)
colAmt =1;
int tempCol = rows;
int tempSum=0;
rows = 3;
for(int t=0;t<rows;t++)
{
for(int col = 0; col<colAmt; col++)
{
tempCol=0;
tempSum =0;
for(int m=t;m<rows;m++)
{
System.out.println(m+", "+tempCol);
tempSum = tempSum + sumTriangle[m][tempCol];
tempCol++;
if(tempSum<triSum)
{
triSum = tempSum;
}
}
}
colAmt++;
}
当我执行程序时,程序打印出:
(0, 0)
(1, 1)
(2, 2)
(1, 0)
(2, 1)
(1, 0)
(2, 1)
(2, 0)
(2, 0)
(2, 0)
何时应该打印:
(0, 0)
(1, 1)
(2, 2)
(1, 0)
(2, 1)
(1, 1)
(2, 2)
(2, 0)
(2, 1)
(2, 2)
我很确定这是我处理 tempCol 变量的方式有问题,但我不知道如何修复它。 任何帮助表示赞赏。谢谢!
最佳答案
替换即可得到你想要的数字
tempCol=0;
与
tempCol=col;
关于java - 嵌套 for 循环和变量的放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28909387/