C#實現大數相加

來源:互聯網
上載者:User

標籤:turn   build   加法   rgs   ica   儲存   log   blog   names   

在C#中,我們經常需要表示整數。但是,c#的基礎資料型別 (Elementary Data Type)中,最大的long也只能表示-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807之間的數。

如果我們需要表示更大的數,就需要用到一定的演算法來完成。

這次,我給大家分享一下C##的大數運算之加法。

代碼只考慮了正數的整數加法。

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace ConsoleApplication1 8 { 9     class Program10     {11         static void Main(string[] args)12         {13             Console.WriteLine("請輸入第一個加數");14             string oneNum = Console.ReadLine();15             Console.WriteLine("請輸入第二個加數");16             string twoNum = Console.ReadLine();17 18             string result = TwoBigNumAdd(oneNum, twoNum);19             Console.WriteLine(result);20         }21 22         static string TwoBigNumAdd(string a, string b)23         {24             int k = 0;25             List<int> array = new List<int>();26             List<int> one = new List<int>();27             List<int> two = new List<int>();28 29             //將兩個數處理成相同長度的字串,短的小的數字前面補030             for (int i = 0; i < (a.Length > b.Length ? a.Length : b.Length); i++)31             {32                 if (i >= a.Length)33                     one.Insert(i - a.Length, 0);34                 else35                     one.Add(int.Parse(a[i].ToString()));36                 if (i >= b.Length)37                     two.Insert(i - b.Length, 0);38                 else39                     two.Add(int.Parse(b[i].ToString()));40             }41 42             //array集合用於儲存相加的和,所以長度最大也只會比最大的數長度長1,剛開始全部存043             for (int i = 0; i <= (a.Length > b.Length ? a.Length : b.Length); i++)44             {45                 array.Add(0);46             }47 48             //從低位往高位每位開始相加,如果相加 >=10 則進1取餘49             for (int i = (a.Length > b.Length ? a.Length : b.Length) - 1; i >= 0; i--)50             {51                 array[i + 1] += (one[i] + two[i]) % 10;52                 k = (one[i] + two[i]) / 10;53 54                 array[i] += k;55             }56 57             //如果首位為0,則移除58             if (array[0] == 0)59             {60                 array.RemoveAt(0);61             }62 63             //將集合轉換成字串返回64             StringBuilder result = new StringBuilder();65             for (int i = 0; i < array.Count; i++)66             {67                 result.Append(array[i]);68             }69             return result.ToString();70         }71     }72 }

 

C#實現大數相加

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.