大整数相乘

一、乘数和被乘数为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("##############################");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值