已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。
2天前关闭。
这篇文章是昨天编辑并提交审查的。
Improve this question
这里是1个二维数组=〉
给定一个n行m列的2d数组,其中n和m总是奇数。2d数组中有两个不同的部分,第一个部分的所有元素之和称为sum 1,另一个部分的所有元素之和称为sum 2。您必须找到sum 1和sum 2之间的绝对差。
problem Image我们必须找出上图中给出sum 1和sum 2之间的绝对值。
如图所示,sum 1由从右上方开始到中间列的所有元素形成,然后垂直地平分图形,然后到达2d阵列的左下方
类似地,sum 2由左上方-〉中间行中的所有元素形成,然后水平平分图形,然后到达2d阵列的右下方
这里,求和1 = 5 + 4 + 3 + 8 + 13 + 12 + 11 = 56
2 = 1 + 6 + 7 + 8 + 9 + 10 + 15 = 56之和
绝对值(总和1-总和2)= 0
`
public class PracticeNew {
public static void main(String[] args) {
int n=3;
int m=5;
int arr[][]={{1 ,2, 3 ,4 ,5},{6, 7, 8 ,9 ,10},{11 ,12 ,13 ,14 ,15}};
int sum1=0;
int sum2=0;
int top=0,down=n-1,left=0,right=m-1,dir=0;
while(top<=down&&left<=right)
{
if(dir==0)
{
for(int i=top;i<=down/2;i++)
{
System.out.print(arr[i][left]+" ");
}
left++;
for(int i=left;i<=right;i++)
{
System.out.print(arr[top][i]+" ");
}
top++;
}
}
}
}
我试图这样做,但似乎是错误的方法,请帮助我在这一点上。
1条答案
按热度按时间mspsb9vt1#