「java大数字怎么计算」java的大数运算

博主:adminadmin 2022-11-24 21:42:06 57

今天给各位分享java大数字怎么计算的知识,其中也会对java的大数运算进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java中如何对较大的数字进行计算

在Java中针对比较大的数字,有大数类型来进行表示。即BigInteger和BigDecimal两个类。

以BigDecimal为例:

BigDecimal bigDecimalA = new BigDecimal("1234567890123456789012345678901");

BigDecimal bigDecimalB = new BigDecimal("1234567890123456789012345678902");

// 加 +

bigDecimalA = bigDecimalA.add(bigDecimalB);

// 减 -

bigDecimalA = bigDecimalA.subtract(bigDecimalB);

// 乘 *

bigDecimalA = bigDecimalA.multiply(bigDecimalB);

// 除 /

bigDecimalA = bigDecimalA.divide(bigDecimalB);

// ......其他的类似,API里很详细,不再赘述了。

Java实现两个无限大的数的加减乘除运算

根据你的要求,我写了一下代码,如下:package com.fer2005.test;public class AddUtil { /**

* @param a1 大数字1,按数组存储

* @param a2 大数字2,按数组存储

* @return 返回结果数组

*/

public int[] getResult(int[] a1,int[] a2){

//取最大的长度作为返回结果的长度,此时未考虑是否有进位

int length=a1.lengtha2.length?a1.length:a2.length;

//新建未考虑进位的数组结果

int[] tmp_res =new int[length];

int i=0;

//循环相加得到res的按照最短数组相加的结果

while(ia1.lengthia2.length){

i++;

tmp_res[length-i]=a1[a1.length-i]+a2[a2.length-i];

}

//操作完成后,需将长数组的值赋给res

//a1的长度说明a1比a2长度小,res需要获取a2的前几位

if(a1.lengtha2.length){

while(length-i0){

tmp_res[length-i-1]=a2[a2.length-i-1];

i++;

}

}else if(a1.lengtha2.length){

//说明a2比a1长度小,res需要获取a1的前几位

while(length-i0){

tmp_res[length-i-1]=a1[a1.length-i-1];

i++;

}

} //考虑进位问题,如果某一元素大于10,则本身减10,前一元素加1,如果第一个元素大于10,特殊处理。

//需处理相加之和大于10的情况

for(int k=tmp_res.length-1;k0;k--){

if(tmp_res[k]=10){

tmp_res[k-1]=tmp_res[k-1]+1;

tmp_res[k]=tmp_res[k]-10;

}

}

int[] res=new int[length+1];

//首位情况特殊处理

if(tmp_res[0]=10){

res[0]=1;

res[1]=tmp_res[0]-10;

for(int m=1;mtmp_res.length;m++){

res[m+1]=tmp_res[m];

}else{ res=tmp_res; }

}

return res;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

AddUtil addUtil = new AddUtil();

int[] a1= {9,2,3,8,5};

int[] a2={1,9,2,3,9};

for(int j:addUtil.getResult(a1, a2)){

System.out.print(j+",");

}

}}

运用JAVA中大数类实现大数的四则运算

import java.math.BigInteger;

public class BigIntegerGet {

public String getAdd(String Str1,String Str2){

String Str3=new String();

BigInteger BigInt1=new BigInteger(Str1);

BigInteger BigInt2=new BigInteger(Str2);

BigInt1=BigInt1.add(BigInt2);

Str3=BigInt1.toString();

return Str3;

}

public String getSubtract(String Str1,String Str2){

String Str3=new String();

BigInteger BigInt1=new BigInteger(Str1);

BigInteger BigInt2=new BigInteger(Str2);

BigInt1=BigInt1.subtract(BigInt2);

Str3=BigInt1.toString();

return Str3;

}

public String getMultiply(String Str1,String Str2){

String Str3=new String();

BigInteger BigInt1=new BigInteger(Str1);

BigInteger BigInt2=new BigInteger(Str2);

BigInt1=BigInt1.multiply(BigInt2);

Str3=BigInt1.toString();

return Str3;

}

public String getDivide(String Str1,String Str2){

String Str3=new String();

BigInteger BigInt1=new BigInteger(Str1);

BigInteger BigInt2=new BigInteger(Str2);

BigInt1=BigInt1.divide(BigInt2);

Str3=BigInt1.toString();

return Str3;

}

}

java中怎样实现60多位整形数字的运算。并能输出。

import java.math.BigInteger;

import java.util.Scanner;

public class BigIntegerDemo {

public static void main(String[] args) {

String a=(new Scanner(System.in)).next();

BigInteger bigInstance=new BigInteger(a); //实例化一个大数字

//取该大数字加2的操作

System.out.println("加法操作:"+bigInstance.add(new BigInteger("2")));

//取该大数字减2的操作

System.out.println("减法操作:"+bigInstance.subtract(new BigInteger("2")));

//取该大数字乘以2的操作

System.out.println("乘法操作:"+bigInstance.multiply(new BigInteger("2")));

//取该大数字除以2的操作

System.out.println("除法操作:"+bigInstance.divide(new BigInteger("2")));

//取该大数字除以3的商

System.out.println("取商:"+bigInstance.divideAndRemainder(new BigInteger("3"))[0]);

//取该大数字除以3的余数

System.out.println("取余数:"+bigInstance.divideAndRemainder(new BigInteger("3"))[1]);

System.out.println("做2次方操作:"+bigInstance.pow(2)); //取该大数字的2次方

System.out.println("取相反数操作:"+bigInstance.negate()); //取该大数字的相反数

}

}

java大数字怎么计算的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java的大数运算、java大数字怎么计算的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-24,除非注明,否则均为首码项目网原创文章,转载请注明出处。