码迷,mamicode.com
首页 > 编程语言 > 详细

下班无聊写个矩阵小算法,c#

时间:2015-11-20 21:46:22      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

    今天是我正式成为程序员的第十天,也正式开始我的职业生涯,刚来公司要学的东西好多,既要掌握公司的框架(话说公司的框架真是太牛逼了,我真的是服了),还要学习EXT,Linq等等,对未来我是充满的希望,我希望我能成一个牛逼的系统架构师,希望通过写博客来记录我走过的每一步路,记录我的成长。

毕竟工作的时间太短了,也没什么可写的,就写两个矩阵相乘的算法吧,来开启我的博客之旅。

下面是实现的代码(是在控制台里实现的

 1  public class Program
 2     {
 3      
 4         static void Main(string[] args)
 5         {
 6             Console.WriteLine("请输入第一个矩阵的行数");
 7             //这里没有加有效性验证一定要输入数字,输入别的会报错
 8             int row1 = int.Parse(Console.ReadLine());
 9             Console.WriteLine("请输入第一个矩阵的列数:");
10             int column1 = int.Parse(Console.ReadLine());
11             Console.WriteLine("请输入第二个矩阵的行数:");
12             int row2 = int.Parse(Console.ReadLine());
13             Console.WriteLine("请输入第二个矩阵的列数:");
14             int column2 = int.Parse(Console.ReadLine());
15             int[,] array1 =CreateArray(new int[row1,column1]);
16             int[,] array2 = CreateArray(new int[row2, column2]);
17             if (column1 != row2)
18                 Console.WriteLine("这两个矩阵不能相乘!");
19             else
20             {
21                 Console.WriteLine("第一个矩阵为:");
22                 ShowArray(array1);
23                 Console.WriteLine("第二个矩阵为:");
24                 ShowArray(array2);
25                 Console.WriteLine("相乘之后的矩阵!");
26                 ShowArray(ArrayMultiply(array1, array2));
27             }
28             
29            
30             Console.ReadKey();
31             
32             
33            
34         }
35         //两个矩阵相乘
36         public static int[,] ArrayMultiply(int[,] a, int[,] b)
37         {
38          
39                 int[,] result = new int[a.GetLength(0), b.GetLength(1)];
40                 for (int i = 0; i < a.GetLength(0); i++)
41                     for (int j = 0; j < b.GetLength(1); j++)
42                     {
43                         int c = 0;
44                         for (int k = 0; k < a.GetLength(1); k++)
45                             c += a[i,k] * b[k,j];
46                         result[i, j] = c;
47                     }
48                 return result;
49         }
50         //生成矩阵
51         public static int[,] CreateArray(int[,] a)
52         {
53             Random ran = new Random();
54             for(int i=0;i<a.GetLength(0);i++)
55                 for (int j = 0; j < a.GetLength(1); j++)
56                 {
57                     a[i,j] = ran.Next(1, 10);
58                 }
59             return a;
60         }
61         //显示矩阵
62         public static void ShowArray(int[,] a)
63         { 
64             for(int i=0;i<a.GetLength(0);i++)
65             {
66                 for (int j = 0; j < a.GetLength(1); j++)
67                 {
68                     Console.Write(a[i, j] + " ");
69                 }
70                 Console.WriteLine();
71             }
72         }
73     }

结果正确不正确大家可以试一试,可算写完了,可以下班了!!!!

 

下班无聊写个矩阵小算法,c#

标签:

原文地址:http://www.cnblogs.com/woaidingyanandcode/p/4982257.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!