【转载】C# 大数相乘

【转载】http://blog.csdn.net/yeerh/archive/2008/03/19/2196922.aspx

using System;
using System.Text;

namespace BigNumberCalculator
{
    
public class OP
    {
        
public static string Mut(string str1, string str2)
        {
            
int l1 = str1.Length;
            
int l2 = str2.Length;
            
int[] re = new int[l1 + l2];
            
int x;
            
int y;
            
for (int i = l1 - 1; i >= 0; i--)
            {
                x 
= (int)str1[i] - 48;      //等同于x=int.Parse(str1[i].ToString());
                for (int j = l2 - 1; j >= 0; j--)
                {
                    y 
= (int)str2[j] - 48;  //等同于y=int.Parse(str2[j].ToString());
                    re[l1 - i + l2 - j - 2+=  x * y;  //关键,将单个的乘积放入对应的位置..
                }
            }

            
int lastIndex = re.Length - 1;  //最高位所在的位置
              StringBuilder sb = new StringBuilder(re.Length);
            
for (int i = 0; i < lastIndex; i++)
            {
                
int t = re[i];
                
if (t >= 10)
                {
                    re[i 
+ 1]  += t / 10;
                    t 
%= 10;
                    
//re[i] = t;
                }
                sb.Insert(
0, t.ToString());
            }
            
if (re[lastIndex] != 0)
            {
                sb.Insert(
0, re[lastIndex].ToString());
            }
            
return sb.ToString();
        }
    }


原文地址:https://www.cnblogs.com/icebutterfly/p/1440221.html