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();  
        }
    }
}

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

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值