zoukankan      html  css  js  c++  java
  • 计算4的1万次方的结果

    基本的思想就是模拟人手算的方法,来完成大数运算,很简单.

      1using System;
      2using System.Collections;
      3using System.Diagnostics;
      4
      5namespace IBMS.Algorithem
      6{
      7    /// <summary>
      8    /// N2Class
      9    /// </summary>

     10    class N2
     11    {
     12        /// <summary>
     13        /// 应用程序的主入口点。
     14        /// </summary>

     15        [STAThread]
     16        static void Main(string[] args)
     17        {
     18            N2 newObject = new N2();
     19
     20            int baseNum = 4;
     21            int testN = 10000;
     22
     23            Console.WriteLine("Std Math Lib Caculating result:"+Math.Pow(baseNum,testN).ToString());
     24            Console.WriteLine("Waitnow begin to caculate..");
     25
     26            int[] result = newObject.Power( baseNum,testN );
     27            
     28            Console.WriteLine("Caculate Result:");
     29
     30            for(int i=result.Length-1; i>=0; i--)
     31            {
     32                Console.Write(result[i]);
     33            }

     34
     35            Console.WriteLine("");
     36        }

     37
     38        int[] Power(int baseNum, int n)
     39        {
     40            
     41            int [] result; 
     42
     43            if(n<0
     44            {
     45                
     46                throw new Exception("现在还不支持负指数");
     47            }

     48            if(n==0)
     49            {
     50                result= new int[1];
     51                result[0]=1
     52                return result;
     53            }

     54
     55            //获得结果位数
     56            int size = (int)(n*Math.Log10(baseNum))+1;
     57        
     58            result = new int[size];
     59            result[0]=1
     60
     61            for(int i=0; i< n; i++)
     62            {
     63                Multi_Vector(result,baseNum);
     64                //Console.WriteLine("Caculate {0}'s {1} ",baseNum,i+1);
     65            }

     66            
     67            return result;
     68        }

     69
     70        void CheckCarray(int []m,int index)
     71        {
     72            
     73            if(m[index]>=10)
     74            {
     75                //向高位进位,模拟手算
     76                m[index+1]+=m[index]/10;
     77                
     78                //设置当前位
     79                m[index] %= 10;
     80
     81                //检查下一个位时候需要进位
     82                CheckCarray(m,index+1);
     83            }

     84        }

     85
     86        
     87        /// <summary>
     88        /// 矢量和一个整数的乘法运算
     89        /// </summary>
     90        /// <param name="m">矢量</param>
     91        /// <param name="n">乘数</param>

     92        void Multi_Vector(int [] m, int n)
     93        {
     94            int totalBit=0;
     95
     96            for(int i=m.Length-1;i>=0; i--)
     97            {
     98                if(m[i]>0)
     99                {
    100                    totalBit = i+1;    //获得最高位
    101                    break;
    102                }

    103            }

    104            
    105            Debug.Assert(totalBit!=0);
    106
    107            for(int  i=totalBit-1; i>=0; i--)
    108            {
    109                //从高位到低位开始算
    110                int temp = m[i]*n;
    111                //产生一个进位
    112                if(temp>=10)
    113                {    
    114                    //向高位进位,模拟手算
    115                    m[i+1]+=temp/10;
    116                    //检查该位是否还需要进位
    117                    CheckCarray(m,i+1);
    118                    temp %= 10;
    119                }

    120                m[i]= temp;
    121            }

    122        }

    123    }

    124}

    125
  • 相关阅读:
    javax.xml.ws.WebServiceException: Provider com.sun.xml.ws.spi.ProviderImpl not found
    注意资源利用 不然导致资源消耗会很严重
    E212: 不能以写入模式打开 linux
    安装db2 提示不是有效的win32应用程序?
    对苹果“五仁”编程语言Swift的简单分析
    Spoj 1557 Can you answer these queries II 线段树 随意区间最大子段和 不反复数字
    Oracle 学习笔记 14 -- 集合操作和高级子查询
    PHP的curl库代码使用
    AWS OpsWorks新增Amazon RDS支持
    最短编辑距离算法
  • 原文地址:https://www.cnblogs.com/dyj057/p/167296.html
Copyright © 2011-2022 走看看