1.题目
给定两个正整数(不含前导 0),计算它们的和。
输入格式
共两行,每行包含一个整数。
输出格式
共一行,包含所求的和。
数据范围
1
≤
整数长度
≤
100000
1≤整数长度≤100000
1≤整数长度≤100000
输入样例:
12
23
输出样例:
35
2.基本思想
Java中有两个类可以来处理高精度的计算
分别是处理整数的BigInteger
和处理小数的BigDecimal
3.代码实现
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.next(), b = sc.next();
char[] A = new char[a.length()], B = new char[b.length()];
for (int i = a.length() - 1; i >= 0; i--)
A[a.length() - 1 - i] = a.charAt(i);//将大整数a 的数字 按照 各位在前排序 a:【6 5 4】——>A:【4 5 6】
for (int i = b.length() - 1; i >= 0; i--) B[b.length() - 1 - i] = b.charAt(i);
String s = add(A, B);
System.out.println(s);
}
private static String add(char[] A, char[] B) {
StringBuffer sb = new StringBuffer();
int t = 0;
if (A.length < B.length) return add(B, A);
for (int i = 0; i < A.length; i++) {
t += A[i] - '0';//转数字
if (i < B.length) t += B[i] - '0';
sb.append(t % 10);
t /= 10;
}
//判断最后的t
if (t != 0) sb.append(1);
return sb.reverse().toString();
}
}
调用 函数 实现
import java.io.*;
import java.math.BigInteger;
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());
System.out.println(a.add(b));
reader.close();
}
}
4.总结
BigInteger 只可用于整数
构造方法
BigInteger(byte[] val)
将包含BigInteger的二进制补码二进制表达式的字节数组转换为BigInteger
BigInteger(int numBits, Random rnd)
构造一个随机生成的BigInteger,均匀分布在0到(2 numBits - 1)的范围内。
BigInteger(String val)
将BigInteger的十进制字符串表示形式转换为BigInteger。
加法 add( )
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());
System.out.println(a.add(b));
reader.close();
}
}
减法 subtract( )
import java.io.*;
import java.math.BigInteger;
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());
System.out.println(a.subtract(b));
reader.close();
}
}
乘法 multiply( )
import java.io.*;
import java.math.BigInteger;
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());
System.out.println(a.multiply(b));
reader.close();
}
}
除法 divideAndRemainder( )
import java.io.*;
import java.math.BigInteger;
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());
//divide 返回值为 a/b
BigInteger[] c = a.divideAndRemainder(b); //返回值为数组,分别为a/b和a%b
System.out.println(c[0]);
System.out.println(c[1]);
reader.close();
}
}
取余 mod( )
import java.io.*;
import java.math.BigInteger;
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());
System.out.println(a.mod(b));
reader.close();
}
}
BigDecimal 处理浮点数运算
构造方法
BigDecimal(char[] in)
一个转换的字符数组表示 BigDecimal成 BigDecimal ,接受字符作为的相同序列 BigDecimal(String)构造。
BigDecimal(char[] in, int offset, int len)
一个转换的字符数组表示 BigDecimal成 BigDecimal ,接受字符作为的相同序列 BigDecimal(String)构造,同时允许一个子阵列被指定。
BigDecimal(double val)
将 double转换为 BigDecimal ,这是 double的二进制浮点值的精确十进制表示
BigDecimal(int val)
将 int成 BigDecimal
BigDecimal(long val)
将 long成 BigDecimal
BigDecimal(String val)
加法 add( )
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());
System.out.println(a.add(b));
reader.close();
}
}
取余 remainder( )
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());
System.out.println(a.remainder(b));
reader.close();
}
}
除法 divide( )
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());
System.out.println(a.divide(b));
reader.close();
}
}