Azure Lei Zhang的博客

weibo: LeiZhang的微博/QQ: 185165016/QQ群:319036205/邮箱:leizhang1984@outlook.com/TeL:139-161-22926

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

   public String Multiply(string num1, string num2)
        {
            int num1Length = num1.Length;
            int num2Length = num2.Length;

 

            //tranfor the string to int array
            int[] inum1 = new int[num1Length];
            //先将字符串转化为整数数组(每个数字字符减去48即可)
            for (int i = 0; i < num1Length; i++)
            {
                inum1[i] = (int)num1[i] - 48;
            }

            int[] inum2 = new int[num2Length];
            for (int i = 0; i < num2Length; i++)
            {
                inum2[i] = (int)num2[i] - 48;
            }

            int[] result = new int[num1Length + num2Length];
            for (int i = num1Length - 1; i >= 0; i--)
            {
                for (int j = num2Length - 1; j >= 0; j--)
                {
                    result[i + j + 1] += inum1[i] * inum2[j];
                    result[i + j] += result[i + j + 1] / 10;
                    result[i + j + 1] %= 10;
                }
            }
            char[] temp = new char[num1Length + num2Length];
            for (int i = 0; i < (num1Length + num2Length); i++)
            {
                temp[i] = (char)(result[i] + 48);
            }
            String sResult = new String(temp);
            return sResult;
        }

posted on 2010-09-02 21:19  Lei Zhang的博客  阅读(364)  评论(0编辑  收藏  举报