`
IThead
  • 浏览: 421091 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

大整数相乘

    博客分类:
  • Java
阅读更多
一、乘数和被乘数为long类型的
public class BigNumberChengLong {

    public static void main(String[] args) {
        long p1 = 1234567890;
        long p2 = 1234567890;
        int result[] = new int[18];//保存结果
        for(int i:result){
            result[i] = 0;
        }

        int pi1[] = new int [getLength(p1)];
        int pi2[] = new int [getLength(p2)];
        //将乘数和被乘数存入数组中
        long temp = p1;
        int num = 0;
        int i = 0;
        while (temp > 0){
            num = (int) (temp%10);
            pi1[i] = num;
            i ++;
            temp /= 10;
        }
        temp = p2;
        i = 0;
        while (temp > 0){
            num = (int) (temp%10);
            pi2[i] = num;
            i ++;
            temp /= 10;
        }

        //计算
        temp = 0; //临时结果
        int step = 0;//进位
        int time = 0;//次数
        int bit = 0;//位数
        for(int j:pi1){
            time = bit;
            for(int k:pi2){
                temp = j * k;
                temp = temp + result[time];
                if(temp > 9){
                    int m = 0;//进位的次数
                    do{
                        result[time + m] = (int) (temp%10);
                        step = (int) ((temp - temp%10)/10);
                        m ++;
                        temp = result[time + m] + step;
                    }while (temp > 9);
                }else {
                    result[time] = (int) temp;
                }
                time ++;
            }
            bit ++;
        }
        System.out.println("***************************************");
        int length = result.length - 1;
        while(result[length] == 0){
            length --;
        }
        for(int j = length; j >= 0; j --){
            if(result[length] == 0){

            }
            System.out.print(result[j]);
        }
        System.out.println();
        System.out.println("***************************************");
    }

    //获得long类型的长度
    private static int getLength(long temp){
        int i = 0;
        while (temp > 0){
            temp = temp/10;
            i ++;
        }
        return i;
    }

}

二、乘数和被乘数是String类型的
public class BigNumberChengString {

    public static void main(String[] args){
        String p1 = "123456789012345678901234123456789012345678901234";//"123456789012345678901234";
        String p2 = "987654321098765432109876543210987654123456789012345678901234";//"987654321098765432109876543210987654";
        int ASCII = 48;
        char[] result = new char[p1.length() + p2.length()];
        for (int i = 0; i < result.length; i++) {
            result[i] = '0';
        }
        char ctemp = ' ';
        char[] pc1 = p1.toCharArray();
        for (int i = 0; i < pc1.length / 2; i++) {
            ctemp = pc1[i];
            pc1[i] = pc1[pc1.length - 1 - i];
            pc1[pc1.length - 1 - i] = ctemp;
        }
        char[] pc2 = p2.toCharArray();
        for (int i = 0; i < pc2.length / 2; i++) {
            ctemp = pc2[i];
            pc2[i] = pc2[pc2.length - 1 - i];
            pc2[pc2.length - 1 - i] = ctemp;
        }
        int temp = 0;//临时结果
        int step = 0;//进位
        int time = 0;//次数
        int bit = 0;//位数
        for (char c1 : pc1) {
            time = bit;
            for (char c2 : pc2) {
                temp = (c1 - ASCII) * (c2 - ASCII);
                temp = temp + result[time] - ASCII;
                if (temp > 9) {//进位
                    int i = 0;
                    do {
                        result[time + i] = (char) (temp % 10 + ASCII);
                        step = (temp - temp % 10) / 10;
                        i++;
                        temp = result[time + i] - ASCII + step;
                    } while (temp > 9);
                    result[time + i] = (char) (temp + ASCII);
                } else {
                    result[time] = (char) (temp + ASCII);
                }
                time++;
            }
            bit++;
        }
        System.out.println("##############################");
        System.out.print(p1 + " x " + p2 + " = ");
        for (int i = result.length - 1; i >= 0; i--) {
            System.out.print(result[i]);
        }
        System.out.println();
        System.out.println("##############################");
    }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics