另一个 BigInteger 问题。我的代码适用于 int 和 long,但由于 UVa 中的测试用例较大,我需要使用 BigInteger。但我不知道如何使用 BigInteger,它让我发疯!代码甚至没有进入 for 循环。它似乎卡在了条件部分。我尝试使用 for 或 while,他们也有同样的问题。
public class Main{
public static void main(String[] asdf){
Scanner pp = new Scanner(System.in);
int testCases = pp.nextInt();
while(testCases-- > 0){
//BigInteger a = pp.nextBigInteger();
BigInteger low = pp.nextBigInteger();
BigInteger upp = pp.nextBigInteger();
BigInteger max = BigInteger.ZERO;
BigInteger i = low;
//((i.compareTo(upp)==-1)||
//(i.compareTo(upp)==0));
//i.add(BigInteger.ONE))
while((i.compareTo(upp))<0){
if(divCount(i).compareTo(divCount(max))==1){
max = i;
}
i.add(BigInteger.ONE);
}
System.out.println("Between "+low+" and "+upp+", "+max+" has a maximum of "+divCount(max)+" divisors.");
}
}
public static BigInteger divCount(BigInteger n){
BigInteger lim = n;
BigInteger size = BigInteger.ZERO;
BigInteger i = BigInteger.ONE;
while(i.compareTo(lim)<0){
if((n.mod(i).compareTo(BigInteger.ZERO))==0){
lim = n.divide(i);
if(!(lim.equals(i))){
size.add(BigInteger.ONE);
}
size.add(BigInteger.ONE);
}
i.add(BigInteger.ONE);
}
//return size;
return BigInteger.ONE;
}
}