c#矩阵行列式计算//线程同步

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 实现矩阵行列式计算
{
    internal class Program
    {
        static void Main(string[] args)
        {//=================================定义矩阵
            Console.WriteLine("矩阵是:");
            double[,] matrix =  {
               //{ 1, 2,3},{  3, 4,5},{  3, 4,7 }
                { 1, 2,4},{  1, 1,3},{  2, 3,5 }
                     };
            //=================================打印矩阵
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write(matrix[i, j] + " ");
                }
                Console.WriteLine();//输出一行后,换行
            }
            Console.WriteLine();
            //=================================调用计算行列式的静态方法
            double determinant = CalculateDeterminant(matrix);
            Console.WriteLine("矩阵的行列式: " + determinant);
            //=================================计算行列式的静态方法的定义
            Console.WriteLine();
            Console.WriteLine("这是自写程序的结果 ");
            Console.ReadLine();
        }

        static double CalculateDeterminant(double[,] matrix)
        {
            int n = matrix.GetLength(0);
            double determinant = 0;

            if (n == 2)
            {
                // 对于2x2矩阵,直接计算
                determinant = (matrix[0, 0] * matrix[1, 1]) - (matrix[0, 1] * matrix[1, 0]);
            }
            else
            {
                // 对于大于2x2的矩阵,按第一行展开计算
                for (int i = 0; i < n; i++)
                {
                    double[,] subMatrix = new double[n - 1, n - 1];//按第i个元素展开,去掉第i行,第i列,给subMatrix
                    for (int j = 1; j < n; j++)//一共循环n-1次,得到去除第一行第一列的子矩阵
                    {
                        for (int k = 0, l = 0; k < n; k++)
                        {
                            if (k != i)//如果k不等于i,则说明当前列不是第一行的列,将这一列的元素添加到子矩阵中
                            {
                                subMatrix[j - 1, l] = matrix[j, k];//如果当前列不是第一行的列,我们将原矩阵中matrix[j, k]复制到子矩阵subMatrix[j - 1, l]。j-1是子矩阵的行,l是子矩阵的列
                                l++; //放下一个元素
                            }
                        }
                    }
                    determinant += (double)Math.Pow(-1, i) * matrix[0, i] * CalculateDeterminant(subMatrix);//按第i个元素进行行列式计算
                }
            }
            return determinant;
        }
           
    }

}

运形效果:

线程同步:一个线程写缓存,一个线程读缓存.用延时配合

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace 线程间的同步
{
    internal class Program
    {//缓冲区,只能容纳一个字符
        private static char buffer;
        //标识量(缓冲区中已使用的空间,初始值为 0 )
        private static long numberOfUsedSpace = 0;
        static void Main(string[] args)
        {//线程:写者
            Thread writer = new Thread(delegate ()
            {
                string sentence = "无可奈何花落去,似曾相识燕归来,小园香径独徘徊。";
                for (int i = 0; i < 24; i++)
                {//写入数据前检查缓冲区是否有一个字符
                 //如果有,就进行等待,直到缓冲区中的数据被进程 reader 读取为止
                    while (Interlocked.Read(ref numberOfUsedSpace) == 1)
                    { Thread.Sleep(800); }//当缓存中数据被读取后此延时不会执行
                    buffer = sentence[i]; //向缓冲区写入数据
                     
                    Interlocked.Increment(ref numberOfUsedSpace); //写入数据后把缓冲区标记为满(由 0 变为 1)
                }
            });
            //线程:读者
            Thread reader = new Thread(delegate ()
            {
                for (int i = 0; i < 24; i++)
                {//读取数据前检查缓冲区是否为空
                 //如果为空,就进行等待,直到进程 writer 向缓冲区中写入数据为止
                    while (Interlocked.Read(ref numberOfUsedSpace) == 0)
                    { Thread.Sleep(200); }
                        char ch = buffer; //从缓冲区读取数据
                    Console.Write(ch);
                    
                    Interlocked.Decrement(ref numberOfUsedSpace);//读取数据后把缓冲区标记为空(由 1 变为 0)

                }
            });
            //启动线程
            writer.Start();
            reader.Start();
            Console.ReadKey();  
        }
    }
}

实际效果:不是一下全部输出的,是一秒输出一个字符.

相关推荐

  1. C++线同步

    2024-02-04 22:36:01       11 阅读
  2. C++(四)--线同步

    2024-02-04 22:36:01       17 阅读
  3. C语言多线编程-线同步

    2024-02-04 22:36:01       33 阅读
  4. c# 多线创建及线同步

    2024-02-04 22:36:01       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-04 22:36:01       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-04 22:36:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-04 22:36:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-04 22:36:01       18 阅读

热门阅读

  1. SQL语言

    SQL语言

    2024-02-04 22:36:01      21 阅读
  2. 进程间通信方式

    2024-02-04 22:36:01       30 阅读
  3. 深度学习的数据集制作、标注、处理相关软件

    2024-02-04 22:36:01       26 阅读
  4. 一文了解Go泛型

    2024-02-04 22:36:01       32 阅读
  5. LaTex字体加粗的方法

    2024-02-04 22:36:01       32 阅读
  6. 【从零开始学设计模式】第二章_单例模式

    2024-02-04 22:36:01       28 阅读
  7. 点灯科技esp32 idfv5.1组件库

    2024-02-04 22:36:01       35 阅读
  8. 河道多参数浮标水质监测站在线分析仪

    2024-02-04 22:36:01       23 阅读
  9. ffmpeg 输入rtsp 输出rtsp

    2024-02-04 22:36:01       27 阅读
  10. 整数二分查找

    2024-02-04 22:36:01       26 阅读