0

我有一个包含随机元素的一维数组,通过 am*n 网格。我想找出其中存在的行总数和列总数。

以下是一维数组的形式:[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

我想这样对待它:

01 02 03 04 05

06 07 08 09 10

11 12 13 14 15

16 17 18 19 20

现在我想找到 reo 总计和列总计。行总计按如下方式完成:

for (int i = 0; i < totalRows; i++)
        {
            for (int j = 0; j < totalColumns; j++)
            {
                rowTotal[i] += numbers[temp + j];
            }               
            temp += totalColumns;
        }

我正在尝试对 Column 做同样的事情。这是代码:

for (int i = 0; i < totalColumns; i++)
        {
            tempk = 0;

            for (int j = 0; j < totalRows; j++)
            {
                blockTotal[i] += numbers[i+j+tempk];
               tempk += totalColumns;

            }
        }

我无法按预期获得列总数。请帮忙。

4

3 回答 3

3

您可以在同一个循环中获得两者

for (int i = 0; i < totalRows; i++)
{
    for (int j = 0; j < totalColumns; j++)
    {
        rowTotal[i] += numbers[i * totalColumns + j];
        blockTotal[j] += numbers[i * totalColumns + j];
    }            
}
于 2013-05-21T12:05:26.567 回答
1

最简单的做法是编写一个将“逻辑”(行、列)地址转换为索引的小方法:

int numberAt(int row, int col)
{
    return numbers[row * totalColumns + col];
}

int[] colTotals = new int[totalColumns];
int[] rowTotals = new int[totalRows];

for (int row = 0; row < totalRows; ++row)
{
    for (int col = 0; col < totalColumns; ++col)
    {
        int number = numberAt(row, col);
        rowTotals[row] += number; 
        colTotals[col] += number;
    }
}

针对以下评论中的问题进行编辑:

这是一个完整的可编译示例,演示了它在非方形数组上的工作:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Demo
{
    class Program
    {
        // Array will 4x3 (rows x cols):
        //
        //  1  2  3 |  6
        //  4  5  6 | 15
        //  7  8  9 | 24
        // 10 11 12 | 33
        // ---------
        // 22 26 30

        int[] numbers = Enumerable.Range(1, 12).ToArray();
        int totalColumns = 3;
        int totalRows    = 4;

        int numberAt(int row, int col)
        {
            return numbers[row * totalColumns + col];
        }

        void test()
        {
            int[] colTotals = new int[totalColumns];
            int[] rowTotals = new int[totalRows];

            for (int row = 0; row < totalRows; ++row)
            {
                for (int col = 0; col < totalColumns; ++col)
                {
                    int number = numberAt(row, col);
                    rowTotals[row] += number;
                    colTotals[col] += number;
                }
            }

            Console.WriteLine("Row totals");

            foreach (int rowTotal in rowTotals)
                Console.Write(rowTotal + " ");

            Console.WriteLine("\nCol totals");

            foreach (int colTotal in colTotals)
                Console.Write(colTotal + " ");

            Console.WriteLine();
        }

        static void Main()
        {
            new Program().test();
        }
    }
}
于 2013-05-21T12:01:01.973 回答
0

按行总计

for(int r = 0; r < totalRows; r++)
{
  for(int c = 0; c < totalColumns; c++)
  {
    rowTotal[r] += numbers[r * totalColumns + c];
  }
}

按列总计

for(int c = 0; c < totalColumns; c++)
{
  for(int r = 0; r < totalRows; r++)
  {
    colTotal[c] += numbers[r * totalColumns + c];
  }
}
于 2013-05-21T12:00:18.390 回答