【问题标题】:two dimensional array sum of columns二维数组的列总和
【发布时间】:2011-10-19 15:30:43
【问题描述】:

我有如下二维数组:

[33] [96] [34] [11] [65] [68] [12] [8] [=327]

[94] [91] [3] [20] [16] [59] [74] [97] [=454]

[29] [0] [31] [13] [4] [63] [52] [73] [=265]

[51] [3] [55] [74] [52] [79] [61] [74] [=449]

[18] [19] [1] [53] [33] [93] [26] [14] [=257]

[56] [41] [4] [16] [45] [8] [57] [22] [=249]

[43] [33] [43] [59] [61] [58] [58] [69] [=424]

[38] [41] [42] [29] [27] [72] [85] [75] [=409]

[36] [3] [23] [65] [40] [56] [41] [96] [=36]

[=?] [=?] [=?] [=?] [=?] [=?] [=?] [=?]

如何获取数组中各列的总和?

这是我获取行总和的代码:

public static void main(String[] args) {
    int[][] nums = new int[9][9];
    int random, total_rows=0, total_cols=0;
    Random randomGenerator = new Random();

    for (int i=0; i < 9; i++) {
        for (int j=0; j < 9; j++) {
            random = randomGenerator.nextInt(100);
            nums[i][j] = random;

            if(j < 8) {
                total_rows += random;
            }else {
                System.out.print("=");
                nums[i][j] = total_rows;
                total_rows = 0;
            }

            System.out.print(nums[i][j] + " ");
        }

        System.out.println();
    }
}

【问题讨论】:

  • 最后一行的总和怎么是36?
  • @Null 应该是360。他把0掉了。

标签: java arrays


【解决方案1】:

你可以试试:

int[][] num = new int[9][9];
/*
 * ...populate the array
 *
 */
for (int i = 0; i < num.length; i++) {
    int sum = 0;
    for (int j = 0; j < num[i].length; j++) {
        sum += num[j][i];
    }
    System.out.println(sum);
}

【讨论】:

    【解决方案2】:

    我会将您的数组生成代码与求和/显示代码分开。
    然后您应该能够注意到,在访问内部循环中的数组时,您所要做的就是翻转 i 和 j。

    【讨论】:

      【解决方案3】:

      家庭作业问题:)

      所以,我不会给你代码,而是告诉你,你已经非常接近答案了。

      要计算列,您需要一个单独的数组,然后,当您在每一行中创建每一列时,只需将该值添加到正确的位置。

      一天结束时,您将获得行总数和列总数。鉴于您到目前为止所做的事情,您应该很容易做到。

      【讨论】:

        【解决方案4】:

        这里是代码;

        二维数组行列总和的演示代码

        import java.util.Random;
        
        public class SumOfColumns {
        
            public static void main(String[] args) {
                int[][] nums = new int[9][9];
        
                handleArray(nums);
        
                printArray(nums);
            }
        
            private static int getSumOfRow(int[][] array, int rowNum) {
                int sumOfRows = 0;
        
                for(int i = 0; i < array[rowNum].length; i++)
                    sumOfRows += array[rowNum][i];
        
                return sumOfRows;
            }
        
            private static int getColumnSum(int[][] array, int columnNum) {
                int sumOfColumn = 0;
        
                for(int i = 0; i < array.length; i++)
                    sumOfColumn += array[i][columnNum];
        
                return sumOfColumn;
            }
        
            private static void handleArray(int[][] array) {
                Random randomGenerator = new Random();
        
                for (int i=0; i < array.length; i++) {
                    for (int j=0; j < array[i].length; j++) {
                        int random = randomGenerator.nextInt(100);
                        array[i][j] = random;
                    }
                }
            }
        
            private static void printArray(int[][] array) {
                System.out.printf("       ");
                for(int i = 0; i < array.length; i++)
                    System.out.printf("%4d ", i);
                System.out.println("\n   __________________________________________________");
        
                for(int i = 0; i < array.length; i++) {
                    System.out.printf("%4d | ", i);
        
                    for(int j = 0; j < array[i].length; j++)
                        System.out.printf("%4d ", array[i][j]);
        
                    System.out.printf("| Sum = %4d", getSumOfRow(array, i));
        
                    System.out.println();
                }
        
                System.out.println("   __________________________________________________");
        
                handleSumOfColumnPrint(array);
            }
        
            private static void handleSumOfColumnPrint(int[][] array) {
                System.out.printf("       ");
        
                for(int i = 0; i < array[0].length; i++)
                    System.out.printf("%4d ", getColumnSum(array, i));
            }
        
        }
        

        样本输出

                  0    1    2    3    4    5    6    7    8 
           __________________________________________________
           0 |    1   86   95   71   78   21   85   99   35 | Sum =  571
           1 |   45   82   68   75   30    1   71   94    8 | Sum =  474
           2 |   33   78   61   74   52   75   63    8    1 | Sum =  445
           3 |   17   10   99   78   61   73   63   40    8 | Sum =  449
           4 |   66   68   54   74   28   78   13    4   79 | Sum =  464
           5 |   75   60   79   76   55   14   20   28   27 | Sum =  434
           6 |   17   17   29   39    6   90    0   94   75 | Sum =  367
           7 |   57   89   73   27   28   42   92   36   16 | Sum =  460
           8 |   40   44   79   64   92   90   43   60   53 | Sum =  565
           __________________________________________________
                351  534  637  578  430  484  450  463  302 
        

        【讨论】:

          【解决方案5】:
          Array[][] // Vertical, Horizontal. 
          
          //sum of row n:
          sum=0
          for(int i:Array[n]){
              sum+=i;
          }
          
          //sum of column n:
          sum=0
          for(int i=0;i<Array.length();i++){
              sum+=Array[i][n];
          }
          

          我希望这是有道理的。

          【讨论】:

            【解决方案6】:

            家庭作业问题有机会吗?

            如果是这样,也许一些提示会更有帮助?

            您在生成 2d 数组时正在计算总行数 - 由于您是逐行执行此操作,因此您可以轻松地计算行数(因为您可以在进行过程中构建它),但是列更棘手,因为您每次迭代都会有效地丢失之前的行。

            如果您仅在第一个 for 循环中生成数组,您将拥有一个完全填充的二维数组,但没有行/列总计。但数据仍保留在您的数组中,因此您现在可以编写代码来计算这些总数。

            作为一个小提示:如果您在生成数据后编写了另一个循环:

            int columnTotal = 0;
            
            for (int r=0; r < 9; r++) {
                columnTotal += nums[r][0];
                }
            

            这应该让您对第一列(第 0 列)的值求和。

            希望这会有所帮助:-)

            【讨论】:

              【解决方案7】:

              这是我解决问题的代码...

              import java.util.Random;
              
              public class columnsAndRowsSummatory
                  {
                  static int x = 0;
                  static int y = 0;
              
                  public static void main (String[] arg)
                  {
                      String spaces = "       " ;
                      //There are 7 spaces on the spaces String
                      int[][] nums = new int[9][9];
                      int random, rowTotal = 0, colTotal = 0;
                      Random randomGenerator = new Random();
                      String ColTotal = "";
              
                  for ( x = 0; x < nums.length -1; x++) 
                  {
                      rowTotal = 0;
                      for (y = 0; y < nums.length ; y++)
                      {
                          random = randomGenerator.nextInt(10000);
                          nums[x][y] = random;
                          while( y < nums.length -1 )
                              {
                              rowTotal = rowTotal + random;
                              break;
                              }
              
                          if(y < nums.length -1 && x != nums.length -1 && random != 0)
                          {
                              int z = (int)Math.floor(Math.log10(random) );
                              System.out.print(random + spaces.substring( (z) ) );
                              //This method calculates how many digits does the number 'random'
                              //Have and takes that amount away from the spaces string to even 
                              //Out numbers displayed
                          }
                          else if(random == 0 && x != nums.length -1)
                          {
                              System.out.print(random + spaces);
                          }
                          else
                          {
                              System.out.printf("=  %d ", rowTotal);
                          }
                      }
                      System.out.println();
                  }
                  for(x = 0; x < nums.length -1; x++)
                  {
                      for (y = 0; y < nums.length -1 ; y++)
                      {
                          colTotal = colTotal + nums[y][x];
                      }
                      int z = (int)Math.floor(Math.log10(colTotal) );
                      ColTotal = ColTotal + colTotal + spaces.substring(z) ;
                      colTotal = 0;
                  }
                  System.out.print(ColTotal + "<-Totals^");
              }
              }
              

              现在,这段代码将显示一个包含您想要多少行和多少列的简洁数组,并且它会在数组的相应最后一行显示列/行总数。如果您要处理大于 1000 万的数字,那么您需要做的就是在空格字符串中添加一个空格,就可以了。

              例如,如果你有

              random = randomGenerator.nextInt(999999);
              

              它会显示如下内容:

              711778  119342  62648   8035    652270  344935  101175  265275  174969  =  2440427 
              414592  798519  453329  988340  180647  540748  572587  675951  903581  =  5528294 
              747535  955957  47966   742898  773810  34212   914212  625716  209569  =  5051875 
              492510  998618  622957  517383  704277  397076  970357  280134  842993  =  5826305 
              190916  57734   590635  944535  991653  366530  145640  402605  456611  =  4146859 
              117598  612710  967466  67209   660383  247281  304953  141144  260186  =  3378930 
              635384  660332  768930  340590  800611  758383  41154   289514  189504  =  4484402 
              173484  740549  226505  921889  135020  555594  554520  65581   433272  =  3806414 
              796617  601478  77503   161865  891582  715084  14614   189920  568788  =  4017451 
              4280414 5545239 3817939 4692744 5790253 3959843 3619212 2935840 4039473 <-Totals^
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2022-12-31
                • 1970-01-01
                • 1970-01-01
                • 2022-01-02
                • 2016-07-05
                • 2013-03-16
                • 1970-01-01
                • 2014-03-20
                相关资源
                最近更新 更多