zoukankan      html  css  js  c++  java
  • 矩阵乘法

      static void Main(string[] args)
            {
                Console.WriteLine("请录入第一个数组:");
                double[,] arr1 = InputArr();
                Console.WriteLine("请录入第二个数组:");
                double[,] arr2 = InputArr();
                int rowsCount = arr1.GetLength(0);
                int colsCount = arr2.GetLength(1);
                int comCount = arr1.GetLength(1);
                int[,] result = new int[rowsCount, colsCount];
                Console.WriteLine("两个数组的乘积为:");
                for (int i = 0; i < rowsCount; i++)
                {
                    for (int j = 0; j < colsCount; j++)
                    {
                        Console.Write("	{0}", GetResult(i, j, comCount, arr1, arr2));
                    }
                    Console.WriteLine();
                }
                Console.ReadLine();
    
            }
            private static double[,] InputArr()
            {
                try
                {
                    Console.WriteLine("请输入数组结构,例如:4,2");
                    string[] arrLen = Console.ReadLine().Split(',');
                    int rowsLen = Convert.ToInt32(arrLen[0]);
                    int colsLen = Convert.ToInt32(arrLen[1]);
                    double[,] result = new double[rowsLen, colsLen];
                    Console.WriteLine("请输入每一行数据,例如:1,2,3,4,7,8");
                    for (int i = 0; i < rowsLen; i++)
                    {
                        string[] row = Console.ReadLine().Split(',');
                        for (int j = 0; j < colsLen; j++)
                        {
                            result[i, j] = Convert.ToDouble(row[j]);
                        }
                    }
                    Console.WriteLine("数组录入完成:");
                    for (int i = 0; i < rowsLen; i++)
                    {
                        for (int j = 0; j < colsLen; j++)
                        {
                            Console.Write("	{0}", result[i, j]);
                        }
                        Console.WriteLine();
                    }
                    return result;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("数据录入异常!");
                    return null;
                }
    
            }
            private static double GetResult(int row, int col, int len, double[,] arr1, double[,] arr2)
            {
                double result = 0;
                for (int temp = 0; temp < len; temp++)
                {
                    result += arr1[row, temp] * arr2[temp, col];
                }
                return result;
            }
  • 相关阅读:
    php 信号量
    .net 反射初体验
    IEnumerable,IQueryable之前世今生
    [SQL]511+512+534+550+569
    [SQL]183+184+185+196+197
    [SQL]3.26--175+176+177+178+180+181+182
    [剑指offer]10.斐波那契数列+青蛙跳台阶问题
    [剑指offer]14-1.剪绳子
    [剑指offer]62.圆圈中最后剩下的数字
    [剑指offer]52.两个链表的第一个公共节点
  • 原文地址:https://www.cnblogs.com/jasonlwings/p/3465056.html
Copyright © 2011-2022 走看看