面试题(两个亿位数相乘)

编程技术  /  houtizong 发布于 3年前   137
<p></p>
<p>public class Text2{</p>
<p><span> </span>//1234567890</p>
<p><span> </span>public static void main(String[] args){</p>
<p><span> </span>//a,b两个任意十位数</p>
<p><span> </span>int[] a=new int[]{1,2,3,4,5,6,7,8,9,0};</p>
<p><span> </span>int[] b=new int[]{1,2,3,4,5,6,7,8,9,0};</p>
<p><span> </span></p>
<p><span> </span>int[] sum=new int[21];<span> </span>//结果</p>
<p><span> </span>int[] sumTemp=new int[21];<span> </span>//临时结果</p>
<p><span> </span></p>
<p><span> </span>//将b数据放入到临时结果中</p>
<p><span> </span>swap(sumTemp,b);</p>
<p><span> </span></p>
<p><span> </span>for (int i = a.length-1; i&gt;=0; i--)</p>
<p><span> </span>{</p>
<p><span> </span>//取出一个数与sumTemp中的每一个相乘</p>
<p><span> </span>for (int j = 0; j&lt;sumTemp.length; j++)</p>
<p><span> </span>{</p>
<p><span> </span>int swap=a[i]*sumTemp[j];</p>
<p><span> </span>sumTemp[j]=swap;</p>
<p><span> </span>whileAdd(sumTemp,j);</p>
<p><span> </span>}</p>
<p><span> </span></p>
<p><span> </span>//将相乘的结果放入结果sum中</p>
<p><span> </span>add(sum,sumTemp);</p>
<p><span> </span></p>
<p><span> </span>//重新将b数据放入到临时结果中</p>
<p><span> </span>swap(sumTemp,b);</p>
<p><span> </span></p>
<p><span> </span>//移位,也就是乘i这个数所在的位数</p>
<p><span> </span>move(sumTemp,a.length-1-i);</p>
<p><span> </span></p>
<p><span> </span>//再把这个sumTemp放入到结果中</p>
<p><span> </span>add(sum,sumTemp);</p>
<p><span> </span></p>
<p><span> </span>}</p>
<p><span> </span></p>
<p><span> </span>//输出结果</p>
<p><span> </span>show(sum);</p>
<p><span> </span></p>
<p><span> </span>}</p>
<p><span> </span></p>
<p><span> </span>//交换</p>
<p><span> </span>public static void swap(int[] a,int[] b)</p>
<p><span> </span>{</p>
<p><span> </span>for (int i = 0; i&lt;a.length; i++)</p>
<p><span> </span>{</p>
<p><span> </span>a[i]=0;</p>
<p><span> </span>}</p>
<p><span> </span>for (int i = 0; i&lt;b.length; i++)</p>
<p><span> </span>{</p>
<p><span> </span>a[a.length-1-i]=b[b.length-1-i];</p>
<p><span> </span>}<span> </span></p>
<p><span> </span>}</p>
<p><span> </span></p>
<p><span> </span>//递归相加</p>
<p><span> </span>public static void whileAdd(int[] n,int index)</p>
<p><span> </span>{</p>
<p><span> </span>if(n[index]&gt;9)</p>
<p><span> </span>{</p>
<p><span> </span>n[index-1]=n[index-1]+(n[index]/10);</p>
<p><span> </span>n[index]=n[index]%10;</p>
<p><span> </span>whileAdd(n,index-1);</p>
<p><span> </span>}</p>
<p><span> </span>}</p>
<p><span> </span></p>
<p><span> </span>//相加</p>
<p><span> </span>public static void add(int[] sum,int[] a)</p>
<p><span> </span>{</p>
<p><span> </span>for (int i = 0; i&lt;a.length; i++)</p>
<p><span> </span>{</p>
<p><span> </span>int swap=a[a.length-1-i] + sum[sum.length-1-i];</p>
<p><span> </span>sum[sum.length-1-i]=swap;</p>
<p><span> </span>whileAdd(sum,sum.length-1-i);</p>
<p><span> </span>}</p>
<p><span> </span>}</p>
<p><span> </span></p>
<p><span> </span>//移位</p>
<p><span> </span>public static void move(int[] n,int index)</p>
<p><span> </span>{</p>
<p><span> </span>int len=0;<span> </span>//找到最高位</p>
<p><span> </span>for (int i = 0; i&lt;n.length; i++)</p>
<p><span> </span>{</p>
<p><span> </span>if(n[i]!=0)</p>
<p><span> </span>{</p>
<p><span> </span>len=i;</p>
<p><span> </span>break;</p>
<p><span> </span>}</p>
<p><span> </span>}</p>
<p><span> </span></p>
<p><span> </span>//从最高位开始移</p>
<p><span> </span>for (int i = len; i&lt;n.length; i++)</p>
<p><span> </span>{</p>
<p><span> </span>n[i-index]=n[i];</p>
<p><span> </span>n[i]=0;</p>
<p><span> </span>}</p>
<p><span> </span>}</p>
<p><span> </span></p>
<p><span> </span>//显示结果</p>
<p><span> </span>public static void show(int[] n)</p>
<p><span> </span>{</p>
<p><span> </span>for (int i = 0; i&lt;n.length; i++)</p>
<p><span> </span>{</p>
<p><span> </span>System.out.print(n[i]);</p>
<p><span> </span>}</p>
<p><span> </span>System.out.println();</p>
<p><span> </span>}</p>
<p>}</p>
<p></p>

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客