高精度 java的一些题

poj 1001 Exponentiation

import java.util.*;
import java.math.*;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNextBigDecimal()) {
			BigDecimal a = in.nextBigDecimal();
			int b = in.nextInt();
			a = a.pow(b);
			a = a.stripTrailingZeros();
			String ans = a.toPlainString();
			if(ans.startsWith("0."))
				ans = ans.substring(1);
			System.out.println(ans);
		}

	}

}

poj 1503 Integer Inquiry

大数加法

``` import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); BigInteger a, b, c; c = BigInteger.valueOf(0); b = BigInteger.valueOf(0); while(true) { a = in.nextBigInteger(); if(a.equals(c)) break; b = b.add(a); } System.out.println(b); }

}

<h3> <a href = "http://acm.hdu.edu.cn/showproblem.php?pid=1042", target = "_blank">hdu 1042 N! </a> </h3>
<h3> 大数乘法 </h3>

import java.util.;
import java.math.
;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BigInteger ans, c;
while(in.hasNextInt()) {
int n = in.nextInt();
ans = BigInteger.valueOf(1);
for(int i = n; i >= 1; i--) {
c = BigInteger.valueOf(i);
ans = ans.multiply(c);
}
System.out.println(ans);
}
}

}


<h3> <a href = "http://acm.hdu.edu.cn/showproblem.php?pid=1316", target = "_blank">hdu 1316 How Many Fibs? </a> </h3>
<h3> 大数乘法, 求给出的两个数之间有多少个fib数</h3>

import java.util.;
import java.math.
;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BigInteger f[] = new BigInteger[505];
f[1] = new BigInteger("1");
f[2] = new BigInteger("2");
for(int i = 3; i<= 500; i++) {
f[i] = f[i-1].add(f[i-2]);
}
BigInteger a, b, zero;
zero = BigInteger.valueOf(0);
while(true) {
a = in.nextBigInteger();
b = in.nextBigInteger();
if(a.equals(zero) && b.equals(zero)) {
break;
}
int tmp = 0;
for(int i = 1; i <= 500; i++) {
if(f[i].compareTo(a)>=0 && f[i].compareTo(b)<=0)
tmp++;
}
System.out.println(tmp);
}
}

}

原文地址:https://www.cnblogs.com/yohaha/p/5331163.html