大数相乘 朱雀 2022-06-02 02:36 237阅读 0赞 在这之前我们先来了解一下Java 中每种基本数据类型所占存储空间的大小。其中 1Byte = 8bit。 <table> <tbody> <tr> <th>基本类型</th> <th>大小</th> <th>最小值</th> <th>最大值</th> <th>包装类型</th> </tr> <tr> <td>boolean</td> <td>-</td> <td>-</td> <td>-</td> <td>Boolean</td> </tr> <tr> <td>char </td> <td> 16-bit </td> <td>Unicode 0</td> <td>Unicode <span></span><span style=""> <span style="width: 3.576em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.829em; height: 0px; font-size: 125%;"><span style="position: absolute; clip: rect(1.709em 1000em 2.883em -0.424em); top: -2.717em; left: 0.003em;"><span><span><span style="display: inline-block; position: relative; width: 1.229em; height: 0px;"><span style="position: absolute; clip: rect(1.923em 1000em 2.883em -0.424em); top: -2.717em; left: 0.003em;"><span style="font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.723em;"></span></span><span style="position: absolute; top: -2.877em; left: 0.483em;"><span><span><span style="font-size: 70.7%; font-family: MathJax_Main;">16</span></span></span><span style="display: inline-block; width: 0px; height: 2.509em;"></span></span></span></span><span style="font-family: MathJax_Main; padding-left: 0.216em;">−</span><span style="font-family: MathJax_Main; padding-left: 0.216em;">1</span></span><span style="display: inline-block; width: 0px; height: 2.723em;"></span></span></span><span style="border-left: 0.003em solid; display: inline-block; overflow: hidden; width: 0px; height: 1.203em; vertical-align: -0.063em;"></span></span> </span></td> <td>Charater</td> </tr> <tr> <td>byte</td> <td> 8 bits </td> <td>-128</td> <td>+127</td> <td>Byte</td> </tr> <tr> <td>short</td> <td> 16 bits </td> <td><span></span><span style=""> <span style="width: 2.563em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.029em; height: 0px; font-size: 125%;"><span style="position: absolute; clip: rect(1.709em 1000em 2.883em -0.371em); top: -2.717em; left: 0.003em;"><span><span style="font-family: MathJax_Main;">−</span><span><span style="display: inline-block; position: relative; width: 1.229em; height: 0px;"><span style="position: absolute; clip: rect(1.923em 1000em 2.883em -0.424em); top: -2.717em; left: 0.003em;"><span style="font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.723em;"></span></span><span style="position: absolute; top: -2.877em; left: 0.483em;"><span><span><span style="font-size: 70.7%; font-family: MathJax_Main;">15</span></span></span><span style="display: inline-block; width: 0px; height: 2.509em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.723em;"></span></span></span><span style="border-left: 0.003em solid; display: inline-block; overflow: hidden; width: 0px; height: 1.203em; vertical-align: -0.063em;"></span></span> </span></td> <td><span></span><span style=""> <span style="width: 4.536em; display: inline-block;"><span style="display: inline-block; position: relative; width: 3.629em; height: 0px; font-size: 125%;"><span style="position: absolute; clip: rect(1.709em 1000em 2.989em -0.424em); top: -2.717em; left: 0.003em;"><span><span style="font-family: MathJax_Main;">+</span><span><span style="display: inline-block; position: relative; width: 1.229em; height: 0px;"><span style="position: absolute; clip: rect(1.923em 1000em 2.883em -0.424em); top: -2.717em; left: 0.003em;"><span style="font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.723em;"></span></span><span style="position: absolute; top: -2.877em; left: 0.483em;"><span><span><span style="font-size: 70.7%; font-family: MathJax_Main;">15</span></span></span><span style="display: inline-block; width: 0px; height: 2.509em;"></span></span></span></span><span style="font-family: MathJax_Main; padding-left: 0.216em;">−</span><span style="font-family: MathJax_Main; padding-left: 0.216em;">1</span></span><span style="display: inline-block; width: 0px; height: 2.723em;"></span></span></span><span style="border-left: 0.003em solid; display: inline-block; overflow: hidden; width: 0px; height: 1.337em; vertical-align: -0.197em;"></span></span> </span></td> <td>Short</td> </tr> <tr> <td>int</td> <td> 32 bits </td> <td><span></span><span style=""> <span style="width: 2.563em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.029em; height: 0px; font-size: 125%;"><span style="position: absolute; clip: rect(1.709em 1000em 2.883em -0.371em); top: -2.717em; left: 0.003em;"><span><span style="font-family: MathJax_Main;">−</span><span><span style="display: inline-block; position: relative; width: 1.229em; height: 0px;"><span style="position: absolute; clip: rect(1.923em 1000em 2.883em -0.424em); top: -2.717em; left: 0.003em;"><span style="font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.723em;"></span></span><span style="position: absolute; top: -2.877em; left: 0.483em;"><span><span><span style="font-size: 70.7%; font-family: MathJax_Main;">31</span></span></span><span style="display: inline-block; width: 0px; height: 2.509em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.723em;"></span></span></span><span style="border-left: 0.003em solid; display: inline-block; overflow: hidden; width: 0px; height: 1.203em; vertical-align: -0.063em;"></span></span> </span></td> <td><span></span><span style=""> <span style="width: 4.536em; display: inline-block;"><span style="display: inline-block; position: relative; width: 3.629em; height: 0px; font-size: 125%;"><span style="position: absolute; clip: rect(1.709em 1000em 2.989em -0.424em); top: -2.717em; left: 0.003em;"><span><span style="font-family: MathJax_Main;">+</span><span><span style="display: inline-block; position: relative; width: 1.229em; height: 0px;"><span style="position: absolute; clip: rect(1.923em 1000em 2.883em -0.424em); top: -2.717em; left: 0.003em;"><span style="font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.723em;"></span></span><span style="position: absolute; top: -2.877em; left: 0.483em;"><span><span><span style="font-size: 70.7%; font-family: MathJax_Main;">31</span></span></span><span style="display: inline-block; width: 0px; height: 2.509em;"></span></span></span></span><span style="font-family: MathJax_Main; padding-left: 0.216em;">−</span><span style="font-family: MathJax_Main; padding-left: 0.216em;">1</span></span><span style="display: inline-block; width: 0px; height: 2.723em;"></span></span></span><span style="border-left: 0.003em solid; display: inline-block; overflow: hidden; width: 0px; height: 1.337em; vertical-align: -0.197em;"></span></span> </span></td> <td>Integer</td> </tr> <tr> <td>long</td> <td> 64 bits </td> <td><span></span><span style=""> <span style="width: 2.563em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.029em; height: 0px; font-size: 125%;"><span style="position: absolute; clip: rect(1.709em 1000em 2.883em -0.371em); top: -2.717em; left: 0.003em;"><span><span style="font-family: MathJax_Main;">−</span><span><span style="display: inline-block; position: relative; width: 1.229em; height: 0px;"><span style="position: absolute; clip: rect(1.923em 1000em 2.883em -0.424em); top: -2.717em; left: 0.003em;"><span style="font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.723em;"></span></span><span style="position: absolute; top: -2.877em; left: 0.483em;"><span><span><span style="font-size: 70.7%; font-family: MathJax_Main;">63</span></span></span><span style="display: inline-block; width: 0px; height: 2.509em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.723em;"></span></span></span><span style="border-left: 0.003em solid; display: inline-block; overflow: hidden; width: 0px; height: 1.203em; vertical-align: -0.063em;"></span></span> </span></td> <td><span></span><span style=""> <span style="width: 4.536em; display: inline-block;"><span style="display: inline-block; position: relative; width: 3.629em; height: 0px; font-size: 125%;"><span style="position: absolute; clip: rect(1.709em 1000em 2.989em -0.424em); top: -2.717em; left: 0.003em;"><span><span style="font-family: MathJax_Main;">+</span><span><span style="display: inline-block; position: relative; width: 1.229em; height: 0px;"><span style="position: absolute; clip: rect(1.923em 1000em 2.883em -0.424em); top: -2.717em; left: 0.003em;"><span style="font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.723em;"></span></span><span style="position: absolute; top: -2.877em; left: 0.483em;"><span><span><span style="font-size: 70.7%; font-family: MathJax_Main;">63</span></span></span><span style="display: inline-block; width: 0px; height: 2.509em;"></span></span></span></span><span style="font-family: MathJax_Main; padding-left: 0.216em;">−</span><span style="font-family: MathJax_Main; padding-left: 0.216em;">1</span></span><span style="display: inline-block; width: 0px; height: 2.723em;"></span></span></span><span style="border-left: 0.003em solid; display: inline-block; overflow: hidden; width: 0px; height: 1.337em; vertical-align: -0.197em;"></span></span> </span></td> <td>Long</td> </tr> <tr> <td>float</td> <td> 32 bits </td> <td>IEEE754</td> <td>IEEE754</td> <td>Float</td> </tr> <tr> <td>double</td> <td> 64 bits </td> <td>IEEE754</td> <td>IEEE754</td> <td>Double</td> </tr> </tbody> </table> 这里实现大数相乘分为两步: 1. 对较大数进行拆分,分别与较小数相乘,逆序保存在数组中 2. 对数组中的数值进行保留与进位操作,使数组中的每一个元素都是小于10的数,完成大数运算 ![这里写图片描述][SouthEast] 代码实现 import java.util.Scanner; public class BigIntegerTest { static long[] array = new long[100]; static int index = array.length - 1; public static void main(String[] args) { Scanner read = new Scanner(System.in); long num1 = read.nextLong(); long num2 = read.nextLong(); boolean flag = false; bigNumMultiply(num1,num2); System.out.print("结果为:"); for(int i=0; i<array.length; i++){ if(array[i] != 0 && flag == false) flag = true; if(flag) System.out.print(array[i]); } } public static void bigNumMultiply(long num1, long num2){ if(num1 < num2){ //始终保持 num1 > num2 long temp = num1; num1 = num2; num2 = temp; } while (num1 > 0){ long num = num1 % 10; //对num1 进行取余,每次获得个位 array[index] = num * num2; //取余后与num2 相乘,倒序保存在数组中 num1 = num1 / 10; index--; } index = array.length - 1; for(int i=array.length-1; i>0; i--){ array[i-1] = array[i-1] + array[i] / 10; //进位 array[i] = array[i] % 10; //保留个位 } } } > 测试 > 99999999999999 > 99999999999999 > 结果为:9999999999999800000000000001 PS:Java 所能支持的最大基本整型数值类型是long ,long类型的数值最大是9223372036854775807。所以这种算法有一个局限就是较小数与较大数分解后相乘的数值不能大于long 类型的最大数值。 [SouthEast]: /images/20220602/32e5b33f04c24657a43936aa295e185a.png
相关 大数相乘 void mul(string a, string b) { int max = 0; int c[1000] = {0}; in r囧r小猫/ 2022年12月04日 08:47/ 0 赞/ 5 阅读
相关 大数相乘 一、背景 最近在看算法的时候发现了一个问题,我们都知道方法的形参是要指定类型的,假如有以下方法 public int example(int a,int b){ 傷城~/ 2022年09月30日 13:55/ 0 赞/ 190 阅读
相关 大数相乘 参考地址:[http://www.cnblogs.com/heyonggang/p/3599857.html][http_www.cnblogs.com_heyonggang_ 悠悠/ 2022年08月20日 06:29/ 0 赞/ 178 阅读
相关 大数相乘 [用分治法实现大数乘法,加法,减法(java实现)][java] 大数乘法即多项式乘法问题,求A(x)与B(x)的乘积C(x),朴素解法的复杂度O 小咪咪/ 2022年08月19日 15:12/ 0 赞/ 167 阅读
相关 大数相乘 无意中看到一个华为面试题,使用代码计算[1234567891011121314151617181920\2019181716151413121110987654321][123 系统管理员/ 2022年08月11日 20:29/ 0 赞/ 181 阅读
相关 大数相乘 题目:请使用代码计算1234567891011121314151617181920\2019181716151413121110987654321。 答: ![复制代码][ Bertha 。/ 2022年08月05日 08:54/ 0 赞/ 193 阅读
相关 大数相乘 在这之前我们先来了解一下Java 中每种基本数据类型所占存储空间的大小。其中 1Byte = 8bit。 <table> <tbody> <tr> <th> 朱雀/ 2022年06月02日 02:36/ 0 赞/ 238 阅读
相关 大数相乘 设X和Y是n位的二进制整数,现在要计算X\Y的结果 将a和b分为两段,每段长均为总长的1/2, ![20180329214901958][] 拼搏现实的明天。/ 2022年05月28日 05:06/ 0 赞/ 203 阅读
相关 大数相乘 题目 编写两个任意位数的大数相乘的程序,给出计算结果。比如: > 题目描述: 输出两个不超过100位的大整数的乘积。 > 输入: 输入两个大整数,如1234567 今天药忘吃喽~/ 2022年05月23日 11:23/ 0 赞/ 323 阅读
相关 大数相乘 def fun(num1,num2): num1 type str num2 type str a = map(int, 落日映苍穹つ/ 2021年10月24日 01:48/ 0 赞/ 319 阅读
还没有评论,来说两句吧...