矩阵,具有最大和的子矩阵

时间:2016-10-16 16:35:24

标签: c#

任务: 1.用m做一个矩阵,并用控制台中的数据填充它。 2.找到具有最大总和的3 * 3子矩阵。

{
    static int[,] ArrayReadConsole()
    {
        Console.WriteLine("please enter n:");
        int n;
        n = Int32.Parse(Console.ReadLine());
        Console.WriteLine("please enter m:");
        int m;
        m = Int32.Parse(Console.ReadLine());
        int[,] data = new int[n, m];
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                Console.WriteLine("please enter a new value");
                int number;
                number = Int32.Parse(Console.ReadLine());
                data[i, j] = number;        
            }
        }
        return data;
    }
    static void SumOfPlatform(int[,] data)
    {
        int sum =0;
        int x = 0;
        int y = 0;
        int maxSum = 0;
        for (int i = 0; i < data.GetLength(0) - 2; i++)
        {
            for (int j = 0; j < data.GetLength(1) - 2; j++)
            {
                for (int k = 0; k < 3; k++)
                {
                    for (int l = 0; l < 3; l++)
                    {
                        sum =+ data[i + k, j + l];
                    }
                }
                if (maxSum < sum)
                {
                    maxSum = sum;
                    x = i;
                    y = j;
                }
                sum =0;
            }
        }
        Console.WriteLine("Sum: {0}\nPosition: {1} {2}",maxSum,x,y );
    }
    static void Main()
    {
        int[,] data = ArrayReadConsole();
        SumOfPlatform(data);
    }
}

} 我写了那段代码,但出了点问题......它没有找到我输入的矩阵的位置或总和。我知道4x for循环是一个坏主意,只是我不想为此制作另一种方法。知道为什么它不起作用吗?

1 个答案:

答案 0 :(得分:0)

您的SumOfPlatform代码只能从= +更改为+ =

static void SumOfPlatform(int[,] data)
        {
            int sum = 0;
            int x = 0;
            int y = 0;
            int maxSum = 0;
            for (int i = 0; i < data.GetLength(0) - 2; i++)
            {
                for (int j = 0; j < data.GetLength(1) - 2; j++)
                {
                    for (int k = 0; k < 3; k++)
                    {
                        for (int l = 0; l < 3; l++)
                        {
                            sum += data[i + k, j + l]; //only Change
                        }
                    }
                    if (maxSum < sum)
                    {
                        maxSum = sum;
                        x = i;
                        y = j;
                    }
                    sum = 0;
                }
            }
            Console.WriteLine("Sum: {0}\nPosition: {1} {2}", maxSum, x, y);
        }
        static void Main(string[] args)
        {
            //  int[,] data = ArrayReadConsole();

            int[,] data = new int[,]
            {
                {1,4,6,7,3,5,7,4 },
                {1,4,5,3,3,5,4,4 },
                {1,1,6,2,1,5,7,4 },
                {1,3,6,3,3,5,2,4 },
                {1,4,6,2,3,5,3,4 },
                {1,4,2,2,3,5,3,4 },
                {1,4,3,3,3,5,2,4 },
                {1,4,4,3,3,5,2,4 }
            };

            SumOfPlatform(data);



        }