Java中两个大数之间的相关运算及BigInteger代码示例

时间:2022-09-13 13:14:16

java中两个大数之间的相关运算及biginteger两段实例代码,具体如下。

大数相减

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.util.scanner;
/*
 进行大数相减,只能对两个正数进行相减
*/
public class bignumber
{
    public static void main(string[] args)
      {
        scanner scan=new scanner(system.in);
        string a,b;
        while (scan.hasnext())
            {
            bignumber big=new bignumber();
            a=scan.nextline();
            b=scan.nextline();
            system.out.println(big.bignumbersub(a,b));
        }
    }
    public string bignumbersub(string x,string y)
      {
        //string result=null;
        char[] a=x.tochararray();
        char[] b=y.tochararray();
        int lena=a.length;
        int lenb=b.length;
        int len=lena>lenb?lena:lenb;
        int[] result=new int[len];
        //字符串反转
        char[] a=new char[lena];
        char[] b=new char[lenb];
        for (int i=0;i<lena;i++)
            {
            a[i]=a[lena-i-1];
        }
        for (int j=0;j<lenb;j++)
            {
            b[j]=b[lenb-j-1];
        }
        //判断最终结果的正负
        char sign='+';
        if (lena<lenb)
            {
            sign='-';
        } else if(lena>lenb)
            {
            sign='+';
        } else
            {
            for (int i=lena-1;i>=0;i--)
                  {
                if (a[i]<b[i])
                        {
                    sign='-';
                    break;
                } else if(a[i]>b[i])
                        {
                    sign='+';
                    break;
                }
            }
        }
        //     
        int aint,bint;
        for (int i=0;i<len;i++)
            {
            aint=i<lena?a[i]-'0':0;
            bint=i<lenb?b[i]-'0':0;
            if (sign=='+')
                  {
                result[i]=aint-bint;
            } else
                  {
                result[i]=bint-aint;
            }
        }
        //借位处理
        for (int j=0;j<len;j++)
            {
            if (result[j]<0)
                  {
                result[j+1]=result[j+1]-1;
                result[j]=result[j]+10;
            }
        }
        //将结果对应为0的位置取消掉
        stringbuilder sb=new stringbuilder();
        boolean flag=true;
        //防止结果集中的地位出现0
        if (sign=='-')
            {
            sb.append(sign);
        }
        for (int i=len-1;i>=0;i--)
            {
            if (result[i]==0&&flag)
                  {
            } else
                  {
                sb.append(result[i]);
                flag=false;
            }
        }
        return sb.tostring();
        //return result;
    }
}

结果:

Java中两个大数之间的相关运算及BigInteger代码示例

在java中,还可以通过biginteger类来解决精度问题。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.scanner;
import java.math.biginteger;
/*
 进行大数相加,
*/
public class bignumber
{
    public static void main(string[] args)
      {
        scanner scan=new scanner(system.in);
        while (scan.hasnext())
            {
            biginteger b1=new biginteger(scan.nextline());
            biginteger b2=new biginteger(scan.nextline());
            system.out.println(b1.add(b2));
            //system.out.println(000);
        }
    }
}

结果

Java中两个大数之间的相关运算及BigInteger代码示例

接着再看一则代码示例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
 * 大数运算
 * biginteger
 * 求91的5次方
 * 求它除以100的余数
 * 大数运算biginteger类的方法调用
 */
package com.test1;
import java.math.*;
import java.math.biginteger;
public class test100 {
 
  /**
   * @param args
   */
  static biginteger k=biginteger.valueof(1);
  static biginteger j=biginteger.valueof(91);
  static biginteger n;
  bigdecimal l=new bigdecimal("100");
  static biginteger m=new biginteger("100");
  public static void main(string[] args) {
    // todo auto-generatedmethod stub
   
//   k=biginteger.valueof(1);
//   k=new biginteger("1");
    for(int i=1;i<=5;i++){
      k=k.multiply(j);
      system.out.println(k.tostring());
//     n=k.remainder(m);
      n=k.remainder(m);
      system.out.println(n.tostring());
      
    }
 
  }
 
}

结果:

Java中两个大数之间的相关运算及BigInteger代码示例

总结

以上就是本文关于java中两个大数之间的相关运算及biginteger代码示例的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。

原文链接:http://www.cnblogs.com/xh0102/p/5767530.html