「java大数乘积」java 大数乘法
今天给各位分享java大数乘积的知识,其中也会对java 大数乘法进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java中如何实现超过整数范围的数据之和与乘积
- 2、用java语言怎么编写两个N位大整数相乘的代码啊?
- 3、如何在JAVA中,输入两个很大的数字使他们相乘后,得到正确结果结果?
- 4、java两个大整数相乘的 算法怎么写
java中如何实现超过整数范围的数据之和与乘积
/*
* BigInteger:可以让超过Integer范围内的数据进行运算
*
* 构造方法:
* BigInteger(String val)
*
/
import java.math.BigInteger;
/*
* public BigInteger add(BigInteger val):加
* public BigInteger subtract(BigInteger val):减
* public BigInteger multiply(BigInteger val):乘
* public BigInteger divide(BigInteger val):除
* public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组
*/
public class BigIntegerDemo {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("100");
BigInteger bi2 = new BigInteger("50");
// public BigInteger add(BigInteger val):加
System.out.println("add:" + bi1.add(bi2));
// public BigInteger subtract(BigInteger val):加
System.out.println("subtract:" + bi1.subtract(bi2));
// public BigInteger multiply(BigInteger val):加
System.out.println("multiply:" + bi1.multiply(bi2));
// public BigInteger divide(BigInteger val):加
System.out.println("divide:" + bi1.divide(bi2));
// public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组
BigInteger[] bis = bi1.divideAndRemainder(bi2);
System.out.println("商:" + bis[0]);
System.out.println("余数:" + bis[1]);
}
}
用java语言怎么编写两个N位大整数相乘的代码啊?
看这个:
1/** *//**
2 * 大整数项乘
3 * @author Administrator
4 *
5 */
6import java.io.BufferedReader;
7import java.io.InputStreamReader;
8import java.io.IOException;
9import java.math.*;
10public class Test1 {
11 public static void main(String[] args){
12 Test1 t1=new Test1();
13
14 long x,y;
15
16 System.out.println("Input x ");
17 x=t1.getNumFromConsole();
18 System.out.println("Input y ");
19 y=t1.getNumFromConsole();
20 System.out.println(t1.mult(x,y,num(x)));
21 }
22 public long getNumFromConsole(){
23 String str=null;
24 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
25 try{
26 str=br.readLine();
27 }catch(IOException ioe){
28 System.out.println(ioe.getMessage());
29 }
30 return Long.parseLong(str);
31 }
32 public long mult(long x,long y,int n){
33 long a,b,c,d,s;
34 int e;
35 if(n==1)
36 return x*y;
37 else{
38 a=(long)(x/Math.pow(10,n/2));//取x左半部分
39 b=(long)(x%Math.pow(10,n/2));//取x的又半部分
40 c=(long)(y/Math.pow(10,n/2));//取y的左半部分
41 d=(long)(y%Math.pow(10,n/2));
42 e=num(a);
43 s=(long)(mult(a,c,e)*Math.pow(10,n)+(mult((a-b),(d-c),e)+mult(a,c,e)+mult(b,d,e))*Math.pow(10, n/2)+mult(b,d,e));
44 return s;
45 }
46 }
47 //判断输入数字的位数
48 private static int num(long x){
49 int i=0;
50 if(x-9=0)
51 return 1;
52 else{
53 while(x!=0){
54 i++;
55 x=x/10;
56 }
57 return i;
58 }
59 }
60}
61
如何在JAVA中,输入两个很大的数字使他们相乘后,得到正确结果结果?
像LZ说的这种计算在实际应用中是常用的.特别是在加密、解密过程中。这个问题的本身就是一个很大的课题。单纯用传统的方式会由于溢出而导致计算结果不正确。目前,针对这一应用,市面上、开源软件中都有一些针对这种运算的函数数,称作大数运算库。我们如果只是为了写应用,而不研究数值计算的话,直接采用这些库就行了。具体的大数运算的库可以上网搜一下。提供一个大数运算的示例程序:
java两个大整数相乘的 算法怎么写
有BigInteger这个类,你可以参数,如果需要自己写,应该是用字符串来处理。
import java.math.BigInteger;
public class TT {
public static void main(String[] args) {
BigInteger i1 = new BigInteger("122222222222222222222222222222222");
BigInteger i2 = new BigInteger("33333333333333333333333333333333");
BigInteger result = i1.multiply(i2);
System.out.println(result.toString());
}
}
关于java大数乘积和java 大数乘法的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-23,除非注明,否则均为
原创文章,转载请注明出处。