High-precision addition, subtraction, multiplication and division in java
- 1. High-precision addition
-
- High-precision addition ideas
- 2. High precision subtraction
-
- High-precision subtraction ideas
- 3. High precision multiplication
-
- High-precision multiplication ideas
- 4. High-precision division
- High-precision division ideas
- 5. High-precision addition, subtraction, multiplication and division of BigInteger and BigDecimal in java
-
- BigIneger handles integer arithmetic:
- BigDecimal handles floating-point arithmetic:
1. High-precision addition
Use code to reflect the operation process
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class Main{<!-- --> public static void main(String[] args) throws IOException {<!-- --> BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String a = bf. readLine(); String b = bf. readLine(); List<Integer> list = add(a, b); for(int i = list.size()-1;i>=0;i--){<!-- --> System.out.print(list.get(i)); } } public static List<Integer> add(String a, String b){<!-- --> int i = a. length()-1; int j = b. length()-1; List<Integer> res = new ArrayList<>(); int t = 0; for(;i>=0||j>=0 ;i--,j--){<!-- --> t + =(i>=0?a.charAt(i)-'0':0) + (j>=0?b.charAt(j)-'0':0); res. add(t); t = t/10; } if(t!=0)res.add(t); return res; } }
High-precision addition ideas
Use the intermediate variable t to indicate the carry situation, t is the value of the current bit, and t/10 is the carry situation of the next bit
It is worth mentioning that in the high-precision storage result, the subscript value is small and the number of storage digits is low, that is, it is stored from the ones digit, and it needs to be fetched from the high digit when obtaining it.
2. High precision subtraction
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class GaoJingJian {<!-- --> public static void main(String[] args) throws IOException {<!-- --> BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String a = bf. readLine(); String b = bf. readLine(); ArrayList<Integer> aa = new ArrayList<>(); ArrayList<Integer> bb = new ArrayList<>(); for(int i = a.length()-1;i>=0;i--){<!-- --> aa.add(a.charAt(i)-'0'); } for(int i = b.length()-1;i>=0;i--){<!-- --> bb.add(b.charAt(i)-'0'); } if(check(a, b)){<!-- --> List<Integer> cc = sub(aa, bb); for(int i = cc.size()-1;i>=0;i--){<!-- --> System.out.print(cc.get(i)); } } else {<!-- --> List<Integer> cc = sub(bb , aa); System.out.print('-'); for(int i = cc.size()-1;i>=0;i--){<!-- --> System.out.print(cc.get(i)); } } } public static boolean check(String a , String b){<!-- --> if(a.length()!=b.length()){<!-- --> return a. length()>b. length(); } for(int i = 0;i<a. length();i ++ ){<!-- --> if(a.charAt(i)!=b.charAt(i)){<!-- --> return a.charAt(i)>b.charAt(i); } } return true; } public static List<Integer> sub(ArrayList<Integer> a , ArrayList<Integer> b){<!-- --> List<Integer> list = new ArrayList<>(); for(int i = 0 , t = 0;i<a. size();i ++ ){<!-- --> t = a.get(i) - t; if(i<b. size()) t = t - b. get(i); list. add((t + 10) ); if(t<0) t = 1; else t = 0; } int i = list. size()-1; while(i>0){<!-- --> if(list.get(i)==0){<!-- --> i--; } else break; } return list.subList(0, i + 1); } }
High-precision subtraction ideas
First judge the size of the two numbers, and decide whether to output a negative sign based on the judgment result
Then use a variable t to represent a forward borrow, (t + 10), if t is an integer, then the number will not change, and finally assign the number to 0, if t is a negative number, then the number will successfully advance one bit Borrow, and finally assign the number to 1.
Leading zeros are removed at the end.
3. High precision multiplication
import java.io.*; import java.util.*; public class Main{<!-- --> public static void main(String[] args) throws IOException{<!-- --> BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String a = br. readLine(); int bb = Integer. valueOf(br. readLine()); ArrayList<Integer> aa = new ArrayList<Integer>(); for (int i = a.length() - 1; i >= 0; i--){<!-- --> aa.add(a.charAt(i) - '0'); } ArrayList<Integer> c = add2(aa,bb); boolean t = true; for(int k = c.size() - 1; k >= 0 ; k--){<!-- --> if(c.get(k) == 0 & amp; & amp; t & amp; & amp; k >= 1) continue; System.out.print(c.get(k)); t = false; } } public static ArrayList<Integer> add2(ArrayList<Integer> aa, int bb){<!-- --> ArrayList<Integer> c = new ArrayList<Integer>(); int t = 0; for(int i = 0; i < aa. size(); i ++ ){<!-- --> t + = aa.get(i) * bb; c.add((t % 10)); t /= 10; } if (t > 0) c. add(t); int i = 0; return c; } }
High-precision multiplication ideas
The difference between multiplication and division and addition and subtraction is that one of the two parameters passed in is high-precision, and the other is low-precision. The high-precision parameter is represented by a set, and the number of each digit is multiplied by the low-precision number. And store the ones in the result set. It is roughly the same as the idea of addition.
4. High precision division
import java.io.*; import java.util.*; public class Main{<!-- --> static int r = 0; public static void main(String[] args) throws IOException{<!-- --> BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String a = br. readLine(); int bb = Integer. valueOf(br. readLine()); ArrayList<Integer> aa = new ArrayList<Integer>(); for (int i = a.length() - 1; i >= 0; i--){<!-- --> aa.add(a.charAt(i) - '0'); } ArrayList<Integer> c = add2(aa,bb); boolean t = true; for(int k = 0; k <c.size() ; k ++ ){<!-- --> if(c.get(k) == 0 & amp; & amp; t & amp; & amp; k <c.size()-1) continue; System.out.print(c.get(k)); t = false; } System.out.println(); System.out.println(r); } public static ArrayList<Integer> add2(ArrayList<Integer> aa, int bb ){<!-- --> ArrayList<Integer> c = new ArrayList<Integer>(); for(int i = aa.size()-1;i>=0;i--){<!-- --> r = r*10 + aa. get(i); c.add(r/bb); r %=bb; } return c; } }
High precision division method
Divide by the divisor from the high bit, and r represents the remainder at the end.
5. High-precision addition, subtraction, multiplication and division of BigInteger and BigDecimal in java
BigIneger handles integer arithmetic:
import java.math.BigInteger; import java.io.*; public class Main {<!-- --> public static void main(String[] args) throws IOException{<!-- --> BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); BigInteger a = new BigInteger(reader. readLine()); BigInteger b = new BigInteger(reader. readLine()); //addition System.out.println(a.add(b)); //subtraction System.out.println(a.subtract(b)); // multiplication System.out.println(a.multiply(b)); //division System.out.println(a.divide(b)); //The return value is a/b System.out.println(a.divideAndRemainder(b)); //The return value is an array, respectively a/b, a%b reader. close(); } }
BigDecimal handles floating-point arithmetic:
import java.io.*; import java.math.BigDecimal; public class Main {<!-- --> public static void main(String[] args) throws IOException {<!-- --> BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); BigDecimal a = new BigDecimal(reader. readLine()); BigDecimal b = new BigDecimal(reader. readLine()); //addition System.out.println(a.add(b)); //subtraction System.out.println(a.subtract(b)); // multiplication System.out.println(a.multiply(b)); //division System.out.println(a.divide(b)); reader. close(); } }
Evaluation: BigInteger and BigDecimal are still easy to use, but if these two classes are disabled in the exam, the idea of high-precision calculation must also be known.