如何在java编程方法中找到纳粹符号?[已结束]

rjee0c15  于 2022-12-02  发布在  Java
关注(0)|答案(1)|浏览(133)

已关闭。此问题需要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++;
           
           }
          
       }


    
    }
}

我试图这样做,但似乎是错误的方法,请帮助我在这一点上。

mspsb9vt

mspsb9vt1#

//There is a 2D array of which's width is described by n, and it's height by m.
//We know the height is described by m, so we can divide our total sum (n * m) amount of numbers into m lines.

//For sum1 we need the very first element, the middle row, and the very last element.

[0][0] + [1][0] + [...] [1][n-1]

//For sum2 we need all the elements from index: 
n / 2 + 1 until n - 1

//(For simplicity's sake: (n / 2) + 1 = x

[0][x] + [0][x + 1] + [...] [0][n-1]
+
[1][x]
+
[2][x] + [2][x-1] + [...] + [2][0]

//The rest should be easy.

相关问题