大数据操作指的是数字非常大,大到已经超过了整个数据类型的保存范围,需要使用对象的形式进行操作。
一、BigInteger
BigInteger表示大的整型数据
import java.math.BigInteger;
public class BigIntegers {
public static void main(String[] args) {
String num1 = "999999999999999999999999999999";
String num2 = "999999999999999999999999999998";
BigInteger big1 = new BigInteger(num1);
BigInteger big2 = new BigInteger(num2);
System.out.println("加法操作:" + big1.add(big2));
System.out.println("减法操作:" + big1.subtract(big2));
System.out.println("乘法操作:" + big1.multiply(big2));
BigInteger result[] = big1.divideAndRemainder(big2);
System.out.println("相除后的商是:" + result[0]);
System.out.println("相除后的余数是:" + result[1]);
}
}
二、BigDecimal
BigDecimal类的主要功能是进行小数的大数计算,而且最重要的是可以精确到指定的四舍五入位数。四舍五入需要用到如下方法:
public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
1.divisor:此BigDecimal要除以的值
2.scale:要返回的BigDecimal商的标度
3.roundingMode:要应用的舍入模式。------ROUND_HALF_UP
import java.math.BigDecimal;
class MyMath{
public static double add(String num1, String num2){
BigDecimal big1 = new BigDecimal(num1);
BigDecimal big2 = new BigDecimal(num2);
return big1.add(big2).doubleValue();
}
public static double sub(String num1, String num2){
BigDecimal big1 = new BigDecimal(num1);
BigDecimal big2 = new BigDecimal(num2);
return big1.subtract(big2).doubleValue();
}
public static double mul(String num1, String num2){
BigDecimal big1 = new BigDecimal(num1);
BigDecimal big2 = new BigDecimal(num2);
return big1.multiply(big2).doubleValue();
}
public static double round(double num, int scale){
BigDecimal bd1 = new BigDecimal(num);
BigDecimal bd2 = new BigDecimal(1);
return bd1.divide(bd2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
public static double div(String num1, String num2, int scale){
BigDecimal bd1 = new BigDecimal(num1);
BigDecimal bd2 = new BigDecimal(num2);
return bd1.divide(bd2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
}
public class BigIntegers {
public static void main(String[] args) {
String num1 = "12345.6789";
String num2 = "3333.23443";
System.out.println("加法操作:" + MyMath.round(MyMath.add(num1, num2), 2));
System.out.println("减法操作:" + MyMath.round(MyMath.sub(num1, num2), 2));
System.out.println("乘法操作:" + MyMath.round(MyMath.mul(num1, num2), 2));
System.out.println("除法操作:" + MyMath.div(num1, num2, 2));
}
}
OJ大数问题:
1.http://acm.hdu.edu.cn/showproblem.php?pid=1002
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
while(scan.hasNextInt()){
int ncase = scan.nextInt();
for(int i = 1; i <= ncase; i++){
BigInteger a = scan.nextBigInteger();
BigInteger b = scan.nextBigInteger();
System.out.println("Case " + i + ":");
System.out.println(a + " + " + b + " = " + a.add(b));
if(i != ncase){
System.out.println();
}
}
}
}
}
2.http://acm.hdu.edu.cn/showproblem.php?pid=1042
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
while(scan.hasNextInt()){
int num = scan.nextInt();
BigInteger bg = BigInteger.ONE;
for(int i = 1; i <= num; i++){
bg = bg.multiply(BigInteger.valueOf(i));
}
System.out.println(bg);
}
}
}
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
int n;
BigInteger tmp, res;
boolean flag = false;
Scanner cin = new Scanner(System.in);
cin.hasNextInt();
n = cin.nextInt();
for (int i = 0; i < n; i++) {
if (flag)
System.out.println();
flag = true;
res = BigInteger.ZERO;
while (cin.hasNextBigInteger()) {
tmp = cin.nextBigInteger();
if (tmp.intValue() == 0)
break;
res = res.add(tmp);
}
System.out.println(res);
}
}
}
4.http://acm.hdu.edu.cn/showproblem.php?pid=1063
import java.math.BigDecimal;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
BigDecimal result;
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
BigDecimal bd = scan.nextBigDecimal();
int n = scan.nextInt();
result = bd.pow(n);
String str = result.stripTrailingZeros().toPlainString();
if(str.charAt(0) == '0'){
System.out.println(str.substring(1));
}else{
System.out.println(str);
}
}
}
}
5.http://acm.hdu.edu.cn/showproblem.php?pid=1753
import java.math.BigDecimal;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
BigDecimal num1 = scan.nextBigDecimal();
BigDecimal num2 = scan.nextBigDecimal();
BigDecimal sum = num1.add(num2);
System.out.println(sum.stripTrailingZeros().toPlainString());
}
}
}
6.http://acm.hdu.edu.cn/showproblem.php?pid=1715
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
BigInteger[]f = new BigInteger[1005];
f[1] = BigInteger.ONE;
f[2] = BigInteger.ONE;
for(int i = 3; i < 1005; i++){
f[i] = f[i-1].add(f[i-2]);
}
while(scan.hasNext()){
int N = scan.nextInt();
for(int i = 1; i <= N; i++){
int index = scan.nextInt();
System.out.println(f[index]);
}
}
}
}
7.http://acm.hdu.edu.cn/showproblem.php?pid=1316
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
BigInteger f[] = new BigInteger[100005];
f[1] = BigInteger.ONE;
f[2] = BigInteger.ONE.add(BigInteger.ONE);
for (int i = 3; i < 10001; i++) {
f[i] = f[i-1].add(f[i-2]);
}
while (scan.hasNext()) {
BigInteger a = scan.nextBigInteger();
BigInteger b = scan.nextBigInteger();
BigInteger count = BigInteger.ZERO;
if (a.equals(BigInteger.ZERO) && b.equals(BigInteger.ZERO)) {
break;
}
else{
for (int i = 1; i < 10001; i++) {
if ((a.compareTo(f[i]) == 0 || a.compareTo(f[i]) == -1)
&& (b.compareTo(f[i]) == 0 || b.compareTo(f[i]) == 1)) {
count = count.add(BigInteger.ONE);
}
}
System.out.println(count);
}
}
}
}