24小时热门版块排行榜    

Znn3bq.jpeg
查看: 839  |  回复: 0

springwu

铁虫 (初入文坛)

[求助] 大家帮忙看一下这个C#求解线性方程组的程序那里有问题?

程序编译没错误,可是算出来的结果不对。小弟才学习C#还请多多指教!
namespace 解线性方程组
{
    class Program
    {
        static void Main(string[] args)
        {
            int amount;//存储方程组的方程个数
            double[,] m;//二维数组用于表示存放方程系数的系数矩阵
            double[] solution;//一维数组用于存放方程组的解
            double t1, t2;
            Console.WriteLine("Input number of equation";//接收用户输入的方程个数
            amount = Convert.ToInt32(Console.ReadLine());
            m = new double[amount, amount + 1];
            solution = new double[amount];

            Console.WriteLine("Input coefficient of equation";
            for (int i = 0; i < amount; i++)
            {
                for (int j = 0; j < amount + 1; j++)
                    m[i, j] = Convert.ToDouble(Console.ReadLine());
            }

            Console.WriteLine("The coefficient of equation is:";
            Console.WriteLine("-------------------------------";
            for (int i = 0; i < amount; i++)
            {
                for (int j = 0; j < amount + 1; j++)
                    Console.Write("{0,8}", m[i, j]);
                Console.WriteLine();
            }
            for (int i = 1; i < amount; i++)
            {
                for (int j = i; j < amount; j++)
                {
                    double coef = -m[j, i - 1] / m[i - 1, i - 1];
                    for (int k = i - 1; k < amount + 1; k++)
                    {
                        m[j, k] += m[i - 1, k] * coef;
                    }
                }
            }
            t1 = m[amount - 1, amount - 1];
            solution[amount - 1] = m[amount - 1, amount] / t1;
            for (int i = amount - 1; i >= 0; i--)
            {
                t2 = 0d;
                for (int j = i + 1; j <= amount - 1; j++)
                {
                    t2 += m[i, j] * solution[j];
                }
                solution = (m[i, amount] - t2) / m[i, i];
            }
            Console.WriteLine();
            Console.WriteLine("\n The result is:";
            Console.WriteLine("-----------------------";
            for (int i = 0; i < amount; i++)
            {
                Console.WriteLine("x[{0}] = 1", i, solution);
            }
            Console.Read();
        }
        
    }

}
回复此楼

» 猜你喜欢

让人迷茫的原因只有一个,那就是本该拼搏的年纪,却想得太多,做得太少
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

智能机器人

Robot (super robot)

我们都爱小木虫

找到一些相关的精华帖子,希望有用哦~

科研从小木虫开始,人人为我,我为人人
相关版块跳转 我要订阅楼主 springwu 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 复试调剂,一志愿郑州大学材料与化工289分 +14 硕星赴 2026-04-08 14/700 2026-04-08 23:59 by GouQ
[考研] 296求调剂 +3 汪!?! 2026-04-08 3/150 2026-04-08 22:00 by zhouyuwinner
[考研] 324求调剂 +17 想上学求调 2026-04-03 17/850 2026-04-08 20:04 by 我减肥1
[考研] 0703调剂,一志愿天津大学319分 +23 haaaabcd 2026-04-05 26/1300 2026-04-08 16:19 by luoyongfeng
[考研] 一志愿生物与医药,296分,求调剂 +14 66鹿 2026-04-03 16/800 2026-04-08 10:38 by tjzhao
[考研] 专硕085403,291分,有两篇专利,一国一奖 +3 哈吉咪哈吉咪 2026-04-07 3/150 2026-04-07 18:21 by 蓝云思雨
[考研] 生物学363调剂求助 +7 fanzhang6666 2026-04-06 9/450 2026-04-07 17:37 by lijunpoly
[考研] 316求调剂 +7 yyx想调剂 2026-04-05 7/350 2026-04-07 14:31 by shdgaomin
[考研] 一志愿武汉理工大学-085601材料工程(专硕)-总分353求调剂 +3 2626262626li 2026-04-02 3/150 2026-04-06 09:08 by 无际的草原
[考研] 262求调剂 +7 天下第一文 2026-04-04 8/400 2026-04-05 21:31 by 激流勇渡
[考研] 341求调剂 +3 学无止境,冲 2026-04-05 3/150 2026-04-05 09:40 by lbsjt
[考研] 278求调剂 +14 范婷娜 2026-04-04 15/750 2026-04-04 22:15 by lqwchd
[考研] 11408 一志愿西电,277分求调剂 +4 zhouzhen654 2026-04-03 4/200 2026-04-04 18:10 by 猪会飞
[考研] 一志愿南农090401,268,求调剂 +5 一木鸟然 2026-04-04 5/250 2026-04-04 17:07 by babysonlkd
[考研] 22408求调剂 354分 可跨专业 +3 hannnnnnn 2026-04-04 3/150 2026-04-04 14:35 by 土木硕士招生
[考研] 化工求调剂 +11 荔香芝士椰奶 2026-04-03 11/550 2026-04-03 22:06 by 啵啵啵0119
[考研] 求调剂 +3 usbdndj 2026-04-03 3/150 2026-04-03 14:10 by dxiaoxin
[考研] 285求调剂 +7 AZMK 2026-04-02 9/450 2026-04-03 11:12 by wanwan00
[考研] 312求调剂 +4 赊月色 2026-04-02 5/250 2026-04-03 08:21 by fangshan711
[考研] 298求B区调剂 +4 zzz,,r 2026-04-02 5/250 2026-04-02 12:17 by 土木硕士招生
信息提示
请填处理意见