zoukankan      html  css  js  c++  java
  • 【转载】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();
            }
        }


  • 相关阅读:
    【win10】浏览器Chrome 和edge 体验对比与使用心得
    【Java】 VM 环境配置过程要点( win10,64位)
    office2013 激活方法
    产品激活 比如Windows激活 , office激活 等激活的原理是什么? KMS等激活工具安全吗?
    回顾外滩踩踏事件,吸取的教训
    【win7】Ubuntu安装使用中的一些注意事项
    黑屏
    mock测试(一)
    Django模型类(一)
    获取本机局域网ip和出口ip
  • 原文地址:https://www.cnblogs.com/icebutterfly/p/1440221.html
Copyright © 2011-2022 走看看