给定两个非负整数(不含前导 0) A , B A,B A,B,请你计算 A / B A/B A/B 的商和余数。
输入格式
共两行,第一行包含整数 A A A,第二行包含整数 B B B。
输出格式
共两行,第一行输出所求的商,第二行输出所求余数。
数据范围
1 ≤ A 的长度 ≤ 100000 , 1≤A的长度≤100000, 1≤A的长度≤100000,
1 ≤ B ≤ 10000 , 1≤B≤10000, 1≤B≤10000,
B 一定不为 0 B 一定不为 0 B一定不为0
输入样例:
7
2
输出样例:
3
1
Java中有两个类可以来处理高精度的计算
分别是处理整数的BigInteger
和处理小数的BigDecimal
调用 函数 实现
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BigInteger A = new BigInteger(sc.next());
BigInteger B = new BigInteger(sc.next());
System.out.println(A.divide(B));
System.out.println(A.mod(B));
}
}
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();
}
}