大数据相乘

  下面是我写的一个关于大数据相乘的算法,核心思想就是通过小学竖式乘法进行运算,具体代码C#如下所示:

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace BigNumberMultiplication
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             try
13             {
14                 int first = 4916;
15                 int second = 12345;
16                 long result = first * second;
17                 Console.WriteLine(string.Format("{0} * {1} = {2}\n\n", first.ToString(), second.ToString(), result.ToString()));
18 
19                 string firstStr = "100000000000000000000";
20                 string secondStr = "100000000000000000000";
21                 string resultStr = MultipFunction(firstStr, secondStr);
22                 Console.WriteLine("The result is: {0}", resultStr.TrimStart('0'));
23                 Console.WriteLine("The length of the result is: {0}", resultStr.TrimStart('0').Length);
24                 Console.ReadKey();
25             }
26             catch (Exception ex)
27             { }
28         }
29 
30         //大数据乘法
31         private static string MultipFunction(string firstNumStr, string secondNumStr)
32         {
33             try
34             {
35                 int firstNumLength = firstNumStr.Length;
36                 int secondNumLength = secondNumStr.Length;
37                 int resultNumLength = firstNumLength + secondNumLength;
38                 int[] firstNumValue = new int[firstNumLength];
39                 int[] secondNumValue = new int[secondNumLength];
40                 int[] resultNumValue = new int[resultNumLength];
41                 //遍历字符串,将每一位的字符转换成为int整形插入整形数组中
42                 for (int i = 0; i < firstNumLength; i++)
43                 {
44                     firstNumValue[i] = firstNumStr[i] - 48;
45                 }
46                 for (int i = 0; i < secondNumLength; i++)
47                 {
48                     secondNumValue[i] = secondNumStr[i] - 48;
49                 }
50                 //定义的整形数组初始化各个位就是0;所以下面赋0的过程可以省略
51                 for(int i = 0; i < resultNumLength; i++)
52                 {
53                     resultNumValue[i] = 0;
54                 }
55 
56                 //算法的核心(小学笔算乘法的流程),将两个数按位进行相乘-->组合成结果的整形数组
57                 //然后对此结果整形数组进行遍历,结果的低位取整附加给临近的高位,低位取余赋给本低位(注:这里说的低位恰好是数组的高位,即数组下标大的位)
58                 for (int i = firstNumLength - 1; i >= 0; i--)
59                 {
60                     for (int j = secondNumLength - 1; j >= 0; j--)
61                     {
62                         resultNumValue[i + j + 1] += firstNumValue[i] * secondNumValue[j];
63                         resultNumValue[i + j] += resultNumValue[i + j +1] /10;
64                         resultNumValue[i + j + 1] = resultNumValue[i + j + 1] % 10;
65                     }
66                 }
67 
68                 //将整形数组转化成字符数组
69                 char[] temp = new char[resultNumLength];
70                 for (int i = 0; i < resultNumLength; i++)
71                 {
72                     temp[i] = (char)(resultNumValue[i] + 48);
73                 }
74                 //将字符数组转化为字符串
75                 string resultStr = new string(temp);
76                 return resultStr;
77             }
78             catch (Exception ex)
79             {
80                 return string.Empty;
81             }
82         }
83     }
84 }
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值