You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# 二维数组矩阵算术运算:实现正元素求和、乘积及最大值后对应计算

C# 矩阵运算实现方案

核心实现逻辑

  • 遍历规则采用行优先顺序,即按行从左到右、从上到下遍历矩阵元素,「最大值之后出现的元素」即该遍历顺序中排在最大值坐标后的元素
  • 乘积初始值设置为1,若没有符合条件的正元素,统一返回乘积为0,避免无意义的1结果
  • 仅做2次矩阵遍历即可完成所有需求,时间复杂度为O(n)(n为矩阵元素总数,此处为25),计算效率最高

完整可运行代码

static void Main(string[] args)
{
    int row = 5;
    int column = 5;
    int[,] array = new int[row, column];
    Random rand = new Random();

    // 生成随机矩阵
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < column; j++)
        {
            array[i, j] = rand.Next(-5, 10);
        }
    }

    // 打印矩阵
    Console.WriteLine("生成的5×5矩阵如下:");
    for (int i = 0; i < array.GetLength(0); i++)
    {
        for (int j = 0; j < array.GetLength(1); j++)
        {
            Console.Write(array[i, j].ToString().PadLeft(3));
        }
        Console.WriteLine();
    }

    // ---------- 需求1:全矩阵正元素求和、求积 + 定位最大值坐标 ----------
    int allPositiveSum = 0;
    int allPositiveProduct = 1;
    int allPositiveCount = 0;
    int maxValue = int.MinValue;
    int maxRowIndex = 0, maxColIndex = 0;

    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < column; j++)
        {
            int current = array[i, j];
            // 统计正元素
            if (current > 0)
            {
                allPositiveSum += current;
                allPositiveProduct *= current;
                allPositiveCount++;
            }
            // 找最大值,有多个相同最大值时取第一个出现的
            if (current > maxValue)
            {
                maxValue = current;
                maxRowIndex = i;
                maxColIndex = j;
            }
        }
    }
    // 处理无正元素的情况
    allPositiveProduct = allPositiveCount == 0 ? 0 : allPositiveProduct;

    // ---------- 需求2:最大值之后的正元素求和、求积 ----------
    int afterMaxPositiveSum = 0;
    int afterMaxPositiveProduct = 1;
    int afterMaxPositiveCount = 0;
    bool passedMax = false;

    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < column; j++)
        {
            // 先判断是否已经过最大值位置
            if (i == maxRowIndex && j == maxColIndex)
            {
                passedMax = true;
                continue;
            }
            if (passedMax)
            {
                int current = array[i, j];
                if (current > 0)
                {
                    afterMaxPositiveSum += current;
                    afterMaxPositiveProduct *= current;
                    afterMaxPositiveCount++;
                }
            }
        }
    }
    afterMaxPositiveProduct = afterMaxPositiveCount == 0 ? 0 : afterMaxPositiveProduct;

    // 输出结果
    Console.WriteLine($"\n所有正元素求和结果:{allPositiveSum}");
    Console.WriteLine($"所有正元素乘积结果:{allPositiveProduct}");
    Console.WriteLine($"\n矩阵最大值为:{maxValue},坐标(行{maxRowIndex+1},列{maxColIndex+1})");
    Console.WriteLine($"最大值之后正元素求和结果:{afterMaxPositiveSum}");
    Console.WriteLine($"最大值之后正元素乘积结果:{afterMaxPositiveProduct}");

    Console.ReadLine();
}

关键逻辑说明

  • 如果需求中多个相同最大值时取最后一个出现的最大值,只需要把找最大值的判断条件从current > maxValue改成current >= maxValue即可
  • 矩阵坐标输出默认做了+1处理,符合日常从1开始数的阅读习惯,如果需要程序原生从0开始的索引,删掉输出处的+1即可

内容的提问来源于stack exchange,提问作者MsnimROD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 09:54:07