「java大数函数」java比较大小函数

博主:adminadmin 2022-12-13 03:39:07 73

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

本文目录一览:

运用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中,如何对大数开根号啊!

java中对于大数BigInteger,BigDecimal开根号没有提供函数,可以参考以下实现方法:

import java.math.BigDecimal;

import java.math.BigInteger;

public class BigSquareRoot {

final static BigInteger HUNDRED = BigInteger.valueOf(100);

public static BigDecimal sqrt(BigDecimal number, int scale, int roundingMode) {

if (number.compareTo(BigDecimal.ZERO) 0)

throw new ArithmeticException("sqrt with negative");

BigInteger integer = number.toBigInteger();

StringBuffer sb = new StringBuffer();

String strInt = integer.toString();

int lenInt = strInt.length();

if (lenInt % 2 != 0) {

strInt = '0' + strInt;

lenInt++;

}

BigInteger res = BigInteger.ZERO;

BigInteger rem = BigInteger.ZERO;

for (int i = 0; i lenInt / 2; i++) {

res = res.multiply(BigInteger.TEN);

rem = rem.multiply(HUNDRED);

BigInteger temp = new BigInteger(strInt.substring(i * 2, i * 2 + 2));

rem = rem.add(temp);

BigInteger j = BigInteger.TEN;

while (j.compareTo(BigInteger.ZERO) 0) {

j = j.subtract(BigInteger.ONE);

if (((res.add(j)).multiply(j)).compareTo(rem) = 0) {

break;

}

}

res = res.add(j);

rem = rem.subtract(res.multiply(j));

res = res.add(j);

sb.append(j);

}

sb.append('.');

BigDecimal fraction = number.subtract(number.setScale(0, BigDecimal.ROUND_DOWN));

int fracLen = (fraction.scale() + 1) / 2;

fraction = fraction.movePointRight(fracLen * 2);

String strFrac = fraction.toPlainString();

for (int i = 0; i = scale; i++) {

res = res.multiply(BigInteger.TEN);

rem = rem.multiply(HUNDRED);

if (i fracLen) {

BigInteger temp = new BigInteger(strFrac.substring(i * 2, i * 2 + 2));

rem = rem.add(temp);

}

BigInteger j = BigInteger.TEN;

while (j.compareTo(BigInteger.ZERO) 0) {

j = j.subtract(BigInteger.ONE);

if (((res.add(j)).multiply(j)).compareTo(rem) = 0) {

break;

}

}

res = res.add(j);

rem = rem.subtract(res.multiply(j));

res = res.add(j);

sb.append(j);

}

return new BigDecimal(sb.toString()).setScale(scale, roundingMode);

}

public static BigDecimal sqrt(BigDecimal number, int scale) {

return sqrt(number, scale, BigDecimal.ROUND_HALF_UP);

}

public static BigDecimal sqrt(BigDecimal number) {

int scale = number.scale() * 2;

if (scale 50)

scale = 50;

return sqrt(number, scale, BigDecimal.ROUND_HALF_UP);

}

public static void main(String args[]) {

BigDecimal num = new BigDecimal("6510354513.6564897413514568413");

long time = System.nanoTime();

BigDecimal root = sqrt(num, 1000);

time = System.nanoTime() - time;

System.out.println(root);

System.out.println(root.pow(2));

System.out.println(time);

}

}

执行结果:

80686.7678969512927493416316741557266722739984372151634715876752358049492663077817843059095146637911180490885758744651273281303288317374885332607051330176028572558172054217029042642080284121950891605518862273493239191320132148293688445347529243846517751025383884710742819252354014378344895438280908159584992112041354808433466321589387318739165992813377399669170549811704076258078653548749003251504791227309054913701062929858500433745971631998487835576600579373929233933246442803804132298016737159672317482520249763464713581048142915509001995943192415694815489364740152312416736301233269587910628885614893125235822493317184917626076223325819402403220531926392808333854523694780539563293133232729900988243013464020440976396084796739002581380094075169287492710301071487312491530290342213569053680461901907481289230152643599970138861788489599118674849815164425194138401918499233009571650761625943781367455101019720348741842171772915942278011905594031830367343193606047124373968951524359600676406162506362881367

6510354513.65648974135145684129999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999261018570188221952291201089398906052231483112444155137889353596770526763176032152876816872483428732651504536335267475372360148274382594285697529542224437785449440331424040251426099649509195223143651828659587364153208947136985285283628868915378240201094213227734756251495878566340409599684823659444904292099882985668906808623177208665251919370268937893457306121802766566270627041680526252368411023459487996551104603913903029501700835230597885867761183071537171958826568685086349210032834841075060512309444622145616551108119893623864579013813710941167261179972571233574705638862140357465569295994523261742960807593601727929262728856153880503561459736300910103299707873770713267018154171165545178430002342459940561678884530151166769964180453998734209554051691222326553700712948508454649608942746651683572634224323098274435576290709769148239120722342126902645574609770558211829972705514153006434846614016006956921594506606758832662240593485962629910320205678474047322232577044567479336985830934534290515788689

46726188

用java编写找最大值函数

你好,好多种办法。我给你写几种经典的排序吧,最大值就在第一个(倒序)或者最后一个(从小到大)。

首先方便输出,定义一个工具类

public class PrintUtil {

public static void print(int[] array){

for(int i=0;iarray.length;i++){

System.out.print(array[i] + " ");

}

System.out.println();

}

}

第一种:冒泡排序

public class BubblingSort {

/*

* 算法思想-升序

* 1、比较相邻两项,若为逆序,则交换

* 2、每次比较的结果就是将最大项浮到数组尾

* 算法分析:

* ------- 最坏情况 --------

* 比较次数:(n-1) + (n-2) + ... + 1 = n*(n-1)/2

* 交换次数:[(n-1) + (n-2) + ... + 1]*3 = 3*n*(n-1)/2

* 所以n元选择排序需要的主要操作次数是:n*(n-1)/2+3*n*(n-1)/2=2*n*n-2*n

* 结论:O(n*n)阶

* ------- 最好情况 --------

* 比较次数:n-1

* 交换次数:0

* 所以n元选择排序需要的主要操作次数是:n-1

* 结论:O(n)阶

*/

public void bubbingSort(int[] array){

int len = array.length ;

for(int i=len-1;i0;i--){

boolean flag = false ;

for(int j=0;ji;j++){

if(array[j]array[j+1]){

int temp = array[j] ;

array[j] = array[j+1] ;

array[j+1] = temp ;

flag = true ;

}

}

if(!flag){

break ;

}

System.out.print("第" + (5-i) + "趟的排序结果为:" );

PrintUtil.print(array) ;

}

}

public static void main(String[] args) {

int[] array = {29,10,14,37,13} ;

// int[] array = {37,29,14,13,10} ; //最差情况

// int[] array = {10,13,14,29,37} ; //最好情况

BubblingSort ss = new BubblingSort();

ss.bubbingSort(array);

PrintUtil.print(array) ;

}

}

第二种:插入排序

public class InsertSort {

/*

* 算法思想-升序

* 1、将一个数组的元素分成两部分,前半部分为有序数组

* 2、每一次取后半部分的第一个元素,将该元素插入到有序数组中

* 算法分析:

* ------- 最坏情况 --------

* 比较次数:1 + 2 + ... + (n-1) = n*(n-1)/2

* 移动次数:

* 内部循环:1 + 2 + ... + (n-1) = n*(n-1)/2

* 外部循环:2*(n-1) OR 2

* 所以n元选择排序需要的主要操作次数是:n*(n-1)/2 + n*(n-1)/2 + 2*(n-1) = n*n+n-2

* 结论:O(n*n)阶

*/

public void insertSort(int[] array){

for (int i = 1; i array.length; i++) {

int temp = array[i] ;

for(int j= i-1; j = 0; j--){

if(temp array[j])

break ;

if(temp array[j]){

array[j+1] = array[j] ;

if( j != 0 temp array[j-1]){

array[j] = temp ;

break ;

}

if(j == 0){

array[0] = temp ;

}

}

}

System.out.print("第" + i + "趟的排序结果为:" );

PrintUtil.print(array) ;

}

}

public static void main(String[] args) {

int[] array = {29,10,14,37,13} ;

InsertSort ss = new InsertSort();

ss.insertSort(array);

PrintUtil.print(array) ;

}

}

第三种:选择排序

public class SelectSort {

/*

* 算法思想-升序

* 1、找到最大的元素,与最后一个元素交换

* 2、除去最后一个元素外,在数组的剩余项中查找最大项,并与倒数第二个元素交换

*

* 算法分析:

* 比较次数:n*(n-1)/2

* 交换次数:3*(n-1)

* 所以n元选择排序需要的主要操作次数是:n*(n-1)/2+3*(n-1)=n*n/2+5*n-3

* 结论:O(n*n)阶

*/

public void selectSort(int[] array){

int len = array.length ; //记录数组的长度

int scope = len ; //遍历范围

for(int i=0;ilen-1;i++){

int max = array[0] ; //数组的最大元素

int index = 0 ; //记录最大元素的下标

for(int j=1;jscope;j++){

if(max array[j]){

max = array[j] ;

index = j ;

}

}

int temp = array[scope-1] ;

array[scope-1] = array[index] ;

array[index] = temp ;

System.out.print("第" + (i+1) + "趟的排序结果为:" );

PrintUtil.print(array) ;

scope -- ;

}

}

public static void main(String[] args) {

int[] array = {29,10,14,37,13} ;

SelectSort ss = new SelectSort();

ss.selectSort(array);

PrintUtil.print(array) ;

}

}

其余的都比较复杂就不给你多写了。

其实很简单的算法,就是遍历这N个数,没遇到一个大的值,就去赋给max,最后输出max,但是这个没什么技术含量,所以在最后说明下。

Java大数类怎么表示小于(或大于)一个数

BigDecimal bda = new BigDecimal("1");

BigDecimal bdb = new BigDecimal("2");

if (bdb.compareTo(bda) 0) {

// bdb bda

}

如果要函数功能,只有自己定义方法了,库中是不提供现有方法的

Parameters:

val BigDecimal to which this BigDecimal is to be compared.

Returns:

-1, 0, or 1 as this BigDecimal is numerically less than, equal to, or greater than val.

JAVA中如何精确进行大数计算

使用Java.math包中的 BigInteger, BigDecimal这两个类。 这两个类可以处理包含任意长度数字序列的数值。BigInteger类实现任意精度的整数运算,BigDicimal实现任意精度的浮点数运算。 语出Core Java

java数组实现大数相加

package com.nileader.big.entity;

public class BigInt {

private String data_str; //原始数据

private int digit; //数据位数

private int[] data; //大数

private boolean carry = false; //进位标识符

/**

* setter and getter

*/

public boolean isCarry() {

return carry;

}

public void setCarry(boolean carry) {

this.carry = carry;

}

public String getData_str() {

return data_str;

}

public void setData_str(String data_str) {

this.data_str = data_str;

}

public int[] getData() {

return data;

}

public void setData(int[] data) {

this.data = data;

}

public int getDigit() {

return digit;

}

public void setDigit(int digit) {

this.digit = digit;

}

//构造方法

public BigInt(){};

public BigInt(String str_data)

{

this.setData_str(str_data);

}

//基本操作

/**

* 形成大数 初始化

*/

public void initBInt()

{

this.setDigit( this.getData_str().length() );

this.data = new int[this.getDigit()];

//将字符组成的大数逆序放入int[] data中

for(int i = 0, j=this.getDigit() ; i

{

// 1104 -- data[0] = '4',data[1] = '0',data[2]=1, data[3]= '1'

this.data[i] = Integer.parseInt( this.getData_str().substring(j-1,j) );

}

}

/**

* 进行大数相加操作

*/

public void add( BigInt bint)

{

//this的位数大于bint的位数

if( this.getDigit() bint.getDigit() )

{

int[] datatemp = this.getData();

this.setData( bint.getData());

bint.setData( datatemp);

this.setDigit(this.getData().length);

bint.setDigit(bint.getData().length);

}

//将短的那个先加完

int i =0;

for(; i

{

int tdata = 0;

//上次运算有进位

if( this.isCarry())

{

tdata = this.getData()[i] + bint.getData()[i] +1;

//取消进位标识

this.setCarry(false);

}

else tdata = this.getData()[i] + bint.getData()[i] ;

//本次结果无进位

if(tdata 10) this.data[i] = tdata;

//本次结果有进位

else if(tdata =10)

{

this.data[i] = tdata -10;

this.setCarry(true);

}

} //短的那个加完了

//剩余数的处理

for(;i

{

//有个进位的

if(this.isCarry())

{

int tdata = this.data[i]+1;

if(tdata =10) this.data[i] = tdata -10;

else

{

this.data[i] = tdata;

this.setCarry(false);

}

}

}

//对最高位益处检测

if(this.data[this.getDigit()-1] == 0)

{

int[] tdata = new int[this.getDigit()+1];

System.arraycopy(this.getData(), 0, tdata, 0, this.getDigit());

tdata[this.getDigit()] = 1;

this.setData(tdata);

}

}

}

其中代码段

//对最高位益处检测

if(this.data[this.getDigit()-1] == 0)

{

int[] tdata = new int[this.getDigit()+1];

System.arraycopy(this.getData(), 0, tdata, 0, this.getDigit());

tdata[this.getDigit()] = 1;

this.setData(tdata);

}

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

The End

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