大整数相乘

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.math.*;
public class Test1 {
public static void main(String[] args){
Test1 t1=new Test1();

long x,y;

System.out.println("Input x ");
x=t1.getNumFromConsole();
System.out.println("Input y ");
y=t1.getNumFromConsole();
System.out.println(t1.mult(x,y,num(x)));
}
public long getNumFromConsole(){
String str=null;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
str=br.readLine();
}catch(IOException ioe){
System.out.println(ioe.getMessage());
}
return Long.parseLong(str);
}
public long mult(long x,long y,int n){
long a,b,c,d,s;
int e;
if(n==1)
return x*y;
else{
a=(long)(x/Math.pow(10,n/2));//取x左半部分
b=(long)(x%Math.pow(10,n/2));//取x的又半部分
c=(long)(y/Math.pow(10,n/2));//取y的左半部分
d=(long)(y%Math.pow(10,n/2));
e=num(a);
s=(long)(mult(a,c,e)*Math.pow(10,n)+(mult((a-b),(d-c),e)+mult(a,c,e)+mult(b,d,e))*Math.pow(10, n/2)+mult(b,d,e));
return s;
}
}
//判断输入数字的位数
private static int num(long x){
int i=0;
if(x-9<=0)
return 1;
else{
while(x!=0){
i++;
x=x/10;
}
return i;
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值