【问题标题】:How to get output and get sum of columns in array / matrix?如何获取输出并获取数组/矩阵中的列总和?
【发布时间】:2020-09-07 05:22:51
【问题描述】:

你好这里的所有代码天才 我会尽量简单地解释我的问题

图像1


要生成 image1,假设需要一个如下所示的数组,请记住,数字从左到右放置在第一行,然后在第二行倒退,如果添加更多数字,它将创建第三行。

int[] something = {1, 2, 3, 2, 1, 2, 1, 3, 3, 1, 1, 2}

所以我想制作一个布局的“地图”,如下面的所需输出。

2 1 1 3 3 1

1 2 3 2 1 2

然后我想从那里找到每一列的总数,就像这样。

2 1 1 3 3 1

1 2 3 2 1 2

..................

3 3 4 5 4 3 

(然后我想将此布局存储在另一个数组中)

希望一切都有意义,如果是这样, 我该怎么做呢?

谢谢大家:)

【问题讨论】:

    标签: java arrays multidimensional-array output


    【解决方案1】:

    看来你可以使用二维数组数据结构来解决这个问题:

    int[][] something = new int[][]{
        {2, 1, 1, 3, 3, 1},
        {1, 2, 3, 2, 1, 2}
    };
    
    int totalForColomn1 = something[0][0] + something [1][0];
    int totalForColomn2 = something[0][1] + something [1][1];
    // ...
    int totoalForColomn6 = something[0][5] + something [1][5];
    

    如果你只能使用一维数组:

    int[] something = new int[] {2, 1, 1, 3, 3, 1, 4, 2, 3, 2, 1, 2};
    int row_size = 6;
    
    int totalForColomn1 = something[0] + something[0 + row_size];
    int totalForColomn2 = something[1] + something[1 + row_size];
    // ...
    int totalForColomn6 = something[5] + something[5 + row_size];
    
    

    请记住通过将那些未决定的元素设置为 0 来保持一致的 row_size
    在这个case 中,你应该像这样初始化你的数组:

    int[] something = new int[] {0, 0, 0, 0, 1, 4, 1, 2, 3, 2, 1, 1};
    

    【讨论】:

    • 感谢您的回复,但是,如果“某物”数组只能是一维的呢
    • 哇,这有点奇怪,让我想想。
    • @5KRU6 你能确保每一行有相同数量的数字吗?如果是的话,可能会更容易。
    • 应该允许行/列有不同数量的数字。假设我们使用这个数组,它产生附加的输出 (imgur.com/a/4xNUImf) int[] something = {1, 2, 3, 2, 1, 1, 4, 1}
    • 所以使用该数组,我的“地图”将是:0 0 0 0 1 4 1 2 3 2 1 1
    【解决方案2】:

    所以如果我没看错,如果 L 是你的数组的长度,你想添加数组的第 n 个和 L-1-n 个元素并将结果存储在一个数组中。我很快一起完成了这个,所以我没有处理如果输入数组的长度是奇数会发生什么(你的问题没有指定)。

    import java.util.Arrays;
    
    public class App {
    
        public static void main(String[] args) {
            int[] something = {1, 2, 3, 2, 1, 2, 1, 3, 3, 1, 1, 2};
            System.out.println(Arrays.toString(addValues(something)));
    
        }
        public static int [] addValues(int [] input){
            int[] output = new int[input.length / 2];
            for(int i = 0; i<input.length/2; i++){
                output[i] = input[i] + input[input.length -1 - i ];
            }
            return output;
        }
    }
    
    

    编辑: 我认为这适用于任意行数的情况。 这个工作原理的主要内容在下面的网格中。

    0  1  2  3  4  5 :row 0
    11 10 9  8  7  6 :row 1
    12 13 14 15 16 17:row 2
    23 22 21 20 19 18:row 3
    

    因此,输出索引是向上还是向下取决于行号,每次我们遇到与输出数组大小相同的输入索引时,我们都需要保持相同的输出索引。

    import java.util.Arrays;
    
    public class App {
    
        public static void main(String[] args) {
            int[] something = { 1, 2, 3, 2, 1, 2, 1, 3, 3, 1, 1, 2 };
            System.out.println(Arrays.toString(addValues(something, 6)));
    
        }
    
        public static int[] addValues(int[] input, int row_lenth) {
            int[] output = new int[row_lenth];
            int output_index = 0;
            for (int i = 0; i < input.length; i++) {
                if (i % row_lenth != 0) {
                    if ((i / row_lenth) % 2 == 0) {
                        output_index++;
                    } else {
                        output_index--;
                    }
                }
                output[output_index] += input[i];
            }
            return output;
        }
    }
    

    【讨论】:

    • 感谢您的回复。这很好用,但正如你提到的(我应该),数组可以是奇数长度
    【解决方案3】:
    import java.util.Scanner;
    public class Stckoverq {
    public static void main(String args[]) {
        Scanner sn = new Scanner(System.in);
        System.out.print("What is the size of array? ");
        int size = sn.nextInt();
        System.out.print("What is length of the row?");
        int len = sn.nextInt();
        int ind = 0, i = 0, j = 0;
        //variable 'ind' is for getting the element from arr[] array at index ind
        int rac[][] = new int[size/len][len];
        //variable 'i' and 'j' is for storing rows and column elements respectively in array rac[]
        int arr[] = new int[size];
        System.out.println("Enter array elements: ");
        for(int k=0;k<size;k++)
            arr[k] = sn.nextInt();
        while(ind!=arr.length)
        {
            if(j==len) {
                j=0;    //Reset column index
                i++;    //Increase row index
            }
            rac[i][j] = arr[ind];
            ind++;
            j++;    //Increase column index
        }
        //Now print the rows and columns................
        for(int r =0;r<size/len;r++) {
            for(int c=0;c<len;c++)
                System.out.print(rac[r][c]+"\t");
            System.out.println();
            }
        int sum[] = new int[len];
        //this array sum[] is used to store sum of all row elements.
        int s = 0;
        for(int c=0;c<len;c++) {
            for(int r =0;r<size/len;r++)
                s += rac[r][c];
            sum[c] = s;
            s = 0;
            }
        for(int x: sum)
            System.out.print(x+"\t");
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      • 2020-01-26
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      相关资源
      最近更新 更多