加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数相乘

发布时间:2020-12-14 03:05:05 所属栏目:大数据 来源:网络整理
导读://今天做了一天,写出来了,但是可以改进,先放着.//大数相乘:用一个整型数组存储乘法结果中每一位上的数字,根据乘法原理进行取余,进位,累加,最后输出结果public class BIGMUL {public static void main(String[] args) {String a = "635660";String b = "11";i

//今天做了一天,写出来了,但是可以改进,先放着.
//大数相乘:用一个整型数组存储乘法结果中每一位上的数字,根据乘法原理进行取余,进位,累加,最后输出结果
public class BIGMUL {
public static void main(String[] args) {
String a = "635660";
String b = "11";
int[] mul = BIGMUL.Mul(a,b);
BIGMUL.show(mul);
}
public static int[] Mul(String a,String b){
int la = a.length();
int lb = b.length();
int[] result = new int[la*lb];//result[]存储每个(个,十,百)位上的数之和
int count = 0,jw = 0;//count 0,1,2表示个位,十位百位,等等,jw进位
int firstNum,secondNum;
for (int i= la-1; i >= 0; i--) {
secondNum = a.charAt(i)-'0';
if(secondNum==0){
continue;
}
for (int j= lb-1; j >= 0; j--) {
firstNum = b.charAt(j)-'0';
count = lb-j-1+la-i-1;
result[count] += (firstNum*secondNum)%10+jw;
jw = (firstNum*secondNum)/10;
if(result[count]>9){
result[count] = result[count]-10;
jw++;
}
}
result[count+1] = jw>0?jw:0;
}
return result;
}
public static void show(int[] mul){
boolean flag = true;
for (int i = mul.length-1; i>=0 ; i--) {
if(flag&mul[i]==0){
}else{
flag = false;
System.out.print(mul[i]);
}
}
}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读