两个大数相乘java,java 两个大数目字的相乘

java 两个大数字的相乘

public testMul() throws Exception {

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("##############################");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值