加法
public class add {
int[] num1;
int[] num2;
int[] res;
public add(String int1,String int2){
int max;
max = int1.length()>int2.length()?int1.length():int2.length();
num1 = new int[max];
num2 = new int[max];
full(num1,max,int1);
full(num2,max,int2);
res = new int[max+1];
for(int i=0;i<res.length;i++)
res[i]=0;
}
public int[] ADD(){
int point = num1.length;
int temp;
for(int i=0;i<point;i++){
temp = num1[point-1-i]+num2[point-1-i]+res[point-i];
if(temp>9){
res[point-i]=temp-10;
res[point-i-1]=1;
continue;
}
res[point-i]=temp;
}
return res;
}
private void full(int[] n,int max,String in){
for(int i=0;i<max;i++)
n[i]=0;
for(int i=max-1,k=in.length()-1;k>=0;i--,k--){
n[i]=in.charAt(k)-48;
}
}
}
乘法
public class mul {
String int1;
String int2;
public mul(String int1,String int2){
this.int1 = int1;
this.int2 = int2;
}
public int[] MUL(){
String temp;
if(int1.length()<int2.length()){
temp = int2;
int2 = int1;
int1 = temp;
}
int[] n1 = new int[int1.length()];
int[] n2 = new int[int2.length()];
int point = n2.length-1;
int[] result =new int[int1.length()+int2.length()];
fullZero(result);
full(n1,n1.length,int1);
full(n2,n2.length,int2);
for(int i=n2.length-1;i>=0;i--){
int[] te = new int[int1.length()+int2.length()];
fullZero(te);
int offset = point-i;
for(int k=n1.length-1;k>=0;k--){
int tt = n1[k]*n2[i]+te[te.length-1-(offset+n1.length-1-k)];
if(tt>9){
te[te.length-1-(offset+n1.length-1-k)] = tt%10;
te[te.length-1-(offset+n1.length-1-k)-1] = (tt - tt%10)/10;
continue;
}
te[te.length-1-(offset+n1.length-1-k)] = tt;
}
result = ad(result,te);
}
return result;
}
public int[] ad(int[] result,int[] te){
int[] rr = new int[te.length+1];
fullZero(rr);
add(result,te,rr);
for(int i=0;i<te.length;i++)
result[i]=rr[i+1];
return result;
}
public void fullZero(int[] te){
for(int i=0;i<te.length;i++)
te[i]=0;
}
private void full(int[] n,int max,String in){
for(int i=0;i<max;i++)
n[i]=0;
for(int i=max-1,k=in.length()-1;k>=0;i--,k--){
n[i]=in.charAt(k)-48;
}
}
public void add(int[] n1,int[] n2,int[] result){
int point = n1.length;
int temp;
for(int i=0;i<point;i++){
temp = n1[point-1-i]+n2[point-1-i]+result[point-i];
if(temp>9){
result[point-i]=temp-10;
result[point-i-1]=1;
continue;
}
result[point-i]=temp;
}
}
}
大整数的乘法与加法
原文作者:大整数乘法问题
原文地址: https://blog.csdn.net/ylqhust/article/details/43504081
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/ylqhust/article/details/43504081
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。