两个大数之间的相关运算

大数相减

  1 import java.util.Scanner;
  2 
  3 /*
  4  进行大数相减,只能对两个正数进行相减
  5 */
  6 
  7 public class BigNumber
  8 {
  9     public static void main(String[] args)
 10     {
 11         Scanner scan=new Scanner(System.in);
 12         String a,b;
 13         while (scan.hasNext())
 14         {
 15             BigNumber big=new BigNumber();
 16             
 17             a=scan.nextLine();
 18             b=scan.nextLine();
 19             
 20             System.out.println(big.bigNumberSub(a,b));
 21         }
 22     }
 23     
 24     public String bigNumberSub(String x,String y)
 25     {
 26         //String result=null;
 27         
 28         char[] a=x.toCharArray();
 29         char[] b=y.toCharArray();
 30         
 31         int lenA=a.length;
 32         int lenB=b.length;
 33         
 34         int len=lenA>lenB?lenA:lenB;
 35         int[] result=new int[len];
 36         
 37         //字符串反转
 38         char[] A=new char[lenA];
 39         char[] B=new char[lenB];
 40         for (int i=0;i<lenA;i++)
 41         {
 42             A[i]=a[lenA-i-1];
 43         }
 44         
 45         for (int j=0;j<lenB;j++)
 46         {
 47             B[j]=b[lenB-j-1];
 48         }
 49         
 50         //判断最终结果的正负
 51         char sign='+';
 52         if (lenA<lenB)
 53         {
 54             sign='-';
 55         }
 56         else if(lenA>lenB)
 57         {
 58             sign='+';
 59         }
 60         else
 61         {
 62             for (int i=lenA-1;i>=0;i--)
 63             {
 64                 if (A[i]<B[i])
 65                 {
 66                     sign='-';
 67                     break;
 68                 }
 69                 else if(A[i]>B[i])
 70                 {
 71                     sign='+';
 72                     break;
 73                 }
 74             }
 75         }
 76         
 77         //            
 78         int aInt,bInt;
 79         for (int i=0;i<len;i++)
 80         {
 81             
 82             aInt=i<lenA?A[i]-'0':0;
 83             bInt=i<lenB?B[i]-'0':0;
 84             
 85             if (sign=='+')
 86             {
 87                 result[i]=aInt-bInt;
 88             }
 89             else 
 90             {
 91                 result[i]=bInt-aInt;
 92             }
 93         }
 94         
 95         //借位处理
 96         for (int j=0;j<len;j++)
 97         {
 98             if (result[j]<0)
 99             {
100                 
101                 result[j+1]=result[j+1]-1;
102                 result[j]=result[j]+10;
103             }
104         }
105         
106         //将结果对应为0的位置取消掉
107         StringBuilder sb=new StringBuilder();
108         boolean flag=true;//防止结果集中的地位出现0
109         
110         if (sign=='-')
111         {
112             sb.append(sign);
113         }
114         for (int i=len-1;i>=0;i--)
115         {
116             if (result[i]==0&&flag)
117             {
118                 
119             }
120             else
121             {
122                 sb.append(result[i]);
123                 flag=false;
124             }
125         }
126         
127         return sb.toString();
128         //return result;
129     }
130 }

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

 1 import java.util.Scanner;
 2 import java.math.BigInteger;
 3 
 4 /*
 5  进行大数相加,
 6 */
 7 
 8 public class BigNumber
 9 {
10     public static void main(String[] args)
11     {
12         Scanner scan=new Scanner(System.in);
13         
14         while (scan.hasNext())
15         {
16         BigInteger  b1=new BigInteger(scan.nextLine());
17         BigInteger  b2=new BigInteger(scan.nextLine());
18         
19         System.out.println(b1.add(b2));
20         //System.out.println(000);
21         }
22     }    
23 }
原文地址:https://www.cnblogs.com/xh0102/p/5767530.html