大数相乘--极简单的思路
发布时间:2020-12-14 02:12:08 所属栏目:大数据 来源:网络整理
导读:大数相乘,面试常见的题型,如何计算两个打算相乘? pre style="font-family: 'Lucida Sans Typewriter'; font-size: 12pt; background-color: rgb(255,255);"pre name="code" class="java"public class Test1 { public void multi(char[] a,char[]b){ int a
大数相乘,面试常见的题型,如何计算两个打算相乘? <pre style="font-family: 'Lucida Sans Typewriter'; font-size: 12pt; background-color: rgb(255,255);"><pre name="code" class="java">public class Test1 { public void multi(char[] a,char[]b){ int alen = a.length; int blen = b.length; //用于存放最后计算出来的结果 int[]s = new int[alen+blen]; for (int i = 0; i < s.length; i++) { s[i] = 0; } //计算,(此时s中的某一个元素的值可能大于9) for (int i = 0; i < alen; i++) { for (int j = 0; j < blen; j++) { s[i+j+1] += (a[i]-'0')*(b[j]-'0'); } } //进位处理 for (int i = alen+blen-1; i >= 0; i--){ if(s[i] >= 10){ s[i-1] += s[i]/10; s[i] %= 10; } } print(s); } public void print(int [] s){ int i = 0; while(s[i] == 0){ i++; } for (; i < s.length; i++){ System.out.print(s[i]); } } public static void main(String[] args) { String a = "36"; String b = "36"; char[] aa = a.toCharArray(); char[] bb = b.toCharArray(); Test1 test1 = new Test1(); test1.multi(aa,bb); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |