高精度加减乘除
非API手写大数据高精度加减乘除
加法:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
String b = sc.nextLine();
int n = a.length();
int m = b.length();
int[] numA = new int[n];
int[] numB = new int[m];
for(int i = n - 1; i >= 0; --i) {
numA[n - i - 1] = a.charAt(i) - '0';
}
for(int i = m - 1; i >= 0; --i) {
numB[m - i - 1] = b.charAt(i) - '0';
}
int[] sum = add(numA, numB);
for(int i = sum.length - 1; i >= 0; --i) {
if(i == sum.length - 1 && sum[i] == 0) continue;
System.out.print(sum[i]);
}
}
public static int[] add(int[] numA, int[] numB) {
int n = numA.length, m = numB.length;
int[] res = new int[Math.max(n, m) + 1];
int carry = 0;
for(int i = 0; i < n || i < m; ++i) {
if(i < n) carry += numA[i];
if(i < m) carry += numB[i];
res[i] = carry % 10;
carry /= 10;
}
if(carry != 0) {
res[Math.max(n, m)] = carry;
}
return res;
}
}
减法:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
String b = sc.nextLine();
int n = a.length();
int m = b.length();
int[] numA = new int[n];
int[] numB = new int[m];
for(int i = n - 1; i >= 0; --i) {
numA[n - i - 1] = a.charAt(i) - '0';
}
for(int i = m - 1; i >= 0; --i) {
numB[m - i - 1] = b.charAt(i) - '0';
}
int[] res = new int[0];
if(compare(numA, numB)) {
res = sub(numA, numB);
} else {
res = sub(numB, numA);
System.out.print("-");
}
boolean flag = true;
for(int i = res.length - 1; i >= 0; --i) {
if(i >= 1 && res[i] == 0 && flag) {
continue;
} else {
flag = false;
}
System.out.print(res[i]);
}
}
public static int[] sub(int[] numA, int[] numB) {
int n = numA.length;
int[] res = new int[n];
int t = 0;
for(int i = 0; i < n; ++i) {
t = numA[i] - t;
if(i < numB.length) {
t -= numB[i];
}
res[i] = (t + 10) % 10;
if(t >= 0) t = 0;
else t = 1;
}
return res;
}
public static boolean compare(int[] a, int[] b) {
if(a.length != b.length) return a.length > b.length;
for(int i = a.length - 1; i >= 0; --i) {
if(a[i] != b[i]) return a[i] > b[i];
}
return true;
}
}
乘法:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
int num2 = sc.nextInt();
List<Integer> num1 = new ArrayList<>();
for(int i = a.length() - 1; i >= 0; --i) {
num1.add(a.charAt(i) - '0');
}
List<Integer> res = mul(num1, num2);
for(int i = res.size() - 1; i >= 0; --i) {
System.out.print(res.get(i));
}
}
public static List<Integer> mul(List<Integer> num1, int num2) {
int t = 0;
List<Integer> res = new ArrayList<>();
for(int i = 0; i < num1.size() || t != 0; ++i) {
if(i < num1.size()) {
t += num1.get(i) * num2;
}
res.add(t % 10);
t /= 10;
}
int idx = res.size() - 1;
while(idx > 0 && res.get(idx) == 0) {
res.remove(idx);
idx--;
}
return res;
}
}
除法:
import java.util.*;
public class Main {
public static int r; // 余数
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
int num2 = sc.nextInt();
List<Integer> num1 = new ArrayList<>();
for(int i = a.length() - 1; i >= 0; --i) {
num1.add(a.charAt(i) - '0');
}
List<Integer> res = div(num1, num2);
for(int i = res.size() - 1; i >= 0; --i) {
System.out.print(res.get(i));
}
System.out.printf("\n%d", r);
}
public static List<Integer> div(List<Integer> num1, int num2) {
List<Integer> res = new ArrayList<>();
for(int i = num1.size() - 1; i >= 0; --i) {
r = r * 10 + num1.get(i);
res.add(r / num2);
r %= num2;
}
Collections.reverse(res);
int idx = res.size() - 1;
while(idx > 0 && res.get(idx) == 0) {
res.remove(idx);
idx--;
}
return res;
}
}