「java大数实现」Java大数

博主:adminadmin 2023-01-15 00:12:08 362

本篇文章给大家谈谈java大数实现,以及Java大数对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java中的大数类实现大数相减

//用BigInteger

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.math.BigInteger;

import java.util.Arrays;

public class MenuNumber {

public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(

System.in));

while(true){

System.out.println("请输入正整数进行相关运算,输入over结束");

String str= br.readLine();

if(str.toLowerCase().equals("over")){

System.out.println("=程序结束=");

break;

}

char[] c=str.toCharArray();//从小到大排序

Arrays.sort(c);//排序

String str_1="",str_2="";

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

str_1+=c[i];//从小到大排序的数字

str_2+=c[c.length-1-i];//从大到小排序的数字

}

try{

//将字符串格式化为数字

BigInteger l1=new BigInteger(str_1);

BigInteger l2=new BigInteger(str_2);

//输出

System.out.println("原 数-"+str+"\n降 序-"+l2+"\n升 序-"+l1+"\n之 差-"+l2.subtract(l1));

}catch(NumberFormatException ne){

System.out.println("请输入正整数,谢谢合作!");//输入的不是数字时的处理。

}

}

}

}

运用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,其实方法都一样,利用数组比如说长度为一千的数字,可以这个 int[] number = new int[1000];相加的思路是,首先两个数组的长度必须是一致,位数不同的话,前面补0,内存有点浪费,但是计算起来比较方便。然后让它们从0到最高位,每位进行相加并保存相应的位置上。最后一步是从0开始判断有没有大于10的数字,如果有就向前面进位(前一个加1,当前位减10),这样就可以。如果是相减,其实思路还是一样的,每位先相减,再判断是否有小于0的,如果有则向前面一位借1(前一位减1,当前位加10)如果是相乘,和上面一样,不过要注意的是进位的时候,不止进一位,比如5*6,就要向前进3位了(前一位加3,当前位减30)除就比较麻烦点,要涉及到精确度问题,得看实际需要 输出就更容易了,直接for循环数组 特别要注意的是,如果最高位有进位的时候,这个是比较容易出错的地方 思路在上面,如果实现不了请留言

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大数的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。