最近做OJ,发现java自带的类库好像没有大整数乘方的方法,后来自己想了下,可以换一种方式实现:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
/**
* 输入2个自然数a、b相乘后的结果的位数
* a,b大于等于1,小于等于2的64次方
* @author Andy
*
*/
public class Oj_10 {
public static void main(String[] args) {
try {
InputStreamReader is_reader = new InputStreamReader(System.in);
String str = new BufferedReader(is_reader).readLine();
int controlFlag = 0, i = 0;
String[] strs = str.split(” “);
BigInteger bn = new BigInteger(strs[0]);
BigInteger bn1 = new BigInteger(strs[1]);
BigInteger bn2 = new BigInteger(“1”);
BigInteger bn3 = new BigInteger(“2”);
BigInteger bn4 = new BigInteger(“1”);
for (i = 0; i < 64; i++) {
bn4=bn4.multiply(bn3);
}
if ((bn.compareTo(bn2) < 0) || (bn1.compareTo(bn2) < 0)
|| (bn.compareTo(bn4) > 0) || (bn1.compareTo(bn4) > 0)) {
controlFlag = 1;
}
if (controlFlag == 0) {
System.out.println(bn.multiply(bn1).toString().length());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}