Chinaunix首页 | 论坛 | 博客
  • 博客访问: 667828
  • 博文数量: 150
  • 博客积分: 4070
  • 博客等级: 中校
  • 技术积分: 1795
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-23 21:44
文章分类

全部博文(150)

文章存档

2012年(1)

2011年(123)

2010年(26)

分类: C/C++

2011-09-06 13:58:34

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;
        }

阅读(1530) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~