大資料相乘

來源:互聯網
上載者:User

標籤:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace BigNumberMultiplication{    class Program    {        static void Main(string[] args)        {            try            {                int first = 4916;                int second = 12345;                long result = first * second;                Console.WriteLine(string.Format("{0} * {1} = {2}\n\n", first.ToString(), second.ToString(), result.ToString()));                string firstStr = "100000000000000000000";                string secondStr = "100000000000000000000";                string resultStr = MultipFunction(firstStr, secondStr);                Console.WriteLine("The result is: {0}", resultStr.TrimStart(‘0‘));                Console.WriteLine("The length of the result is: {0}", resultStr.TrimStart(‘0‘).Length);                Console.ReadKey();            }            catch (Exception ex)            { }        }        //大資料乘法        private static string MultipFunction(string firstNumStr, string secondNumStr)        {            try            {                int firstNumLength = firstNumStr.Length;                int secondNumLength = secondNumStr.Length;                int resultNumLength = firstNumLength + secondNumLength;                int[] firstNumValue = new int[firstNumLength];                int[] secondNumValue = new int[secondNumLength];                int[] resultNumValue = new int[resultNumLength];                //遍曆字串,將每一位的字元轉換成為int整形插入整形數組中                for (int i = 0; i < firstNumLength; i++)                {                    firstNumValue[i] = firstNumStr[i] - 48;                }                for (int i = 0; i < secondNumLength; i++)                {                    secondNumValue[i] = secondNumStr[i] - 48;                }                //定義的整形數組初始化各個位就是0;所以下面賦0的過程可以省略                for(int i = 0; i < resultNumLength; i++)                {                    resultNumValue[i] = 0;                }                //演算法的核心(小學筆算乘法的流程),將兩個數按位進行相乘-->組合成結果的整形數組                //然後對此結果整形數組進行遍曆,結果的低位取整附加給臨近的高位,低位取餘賦給本低位(註:這裡說的低位恰好是數組的高位,即數組下標大的位)                for (int i = firstNumLength - 1; i >= 0; i--)                {                    for (int j = secondNumLength - 1; j >= 0; j--)                    {                        resultNumValue[i + j + 1] += firstNumValue[i] * secondNumValue[j];                        resultNumValue[i + j] += resultNumValue[i + j +1] /10;                        resultNumValue[i + j + 1] = resultNumValue[i + j + 1] % 10;                    }                }                //將整形數組轉化成字元數組                char[] temp = new char[resultNumLength];                for (int i = 0; i < resultNumLength; i++)                {                    temp[i] = (char)(resultNumValue[i] + 48);                }                //將字元數組轉化為字串                string resultStr = new string(temp);                return resultStr;            }            catch (Exception ex)            {                return string.Empty;            }        }    }}

  

大資料相乘

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.