• 【转载】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();
            }
        }


  • 相关阅读:
    java NIO的基本用法
    BufferedReader源码分析
    FileInputStream读中文乱码问题
    Java使用IO流对同一个文件进行读写操作
    php表格--大数据处理
    tp5--模型关联
    tp5 -- join注意事项
    表单外部提交
    网站防止sql注入
    微信网页授权报code been used, hints: [ req_id: XYv1Ha07042046 ]
  • 原文地址:https://www.cnblogs.com/icebutterfly/p/1440221.html
Copyright © 2020-2023  润新知