2012-04-30 2 views
0

또 다른 BigInteger 문제입니다. 내 코드는 int와 long으로 작동하지만, UVa의 테스트 케이스가 더 크기 때문에 BigInteger를 사용해야합니다. 하지만 BigInteger를 사용하는 방법을 모르겠다. 코드는 for-loop에도 들어 가지 않습니다. 조건부에 붙어있는 것 같습니다. for 또는 while을 사용해 보았는데 동일한 문제가 있습니다.for 루프 조건에서 BigInteger를 사용하고 있지 않습니다. 어떻게해야합니까?

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; 
    } 
} 
+2

기본적으로,'i.add (BigInteger.ONE)'은 no-op입니다. 대신에 i = i.add (BigInteger.ONE)를 사용하고 다른 곳에서는 똑같은 일을하십시오. –

+2

for for 루프는 무엇입니까? 아무 것도 볼 수 없다. – mata

+0

추가 조언으로서 :이 알고리즘을 사용하여 "Accepted"를 얻지 못할 수도 있습니다. ** 프라임 인수 분해 **를 사용해보십시오 ** 키워드를 사용하여 검색하십시오 ** ** Number Theory' **, **' Count Divisors' **. 당신이 아마 ** TimeLimitExceeded **를 얻으려는 다른 (아닌) 현명한. 나는 UVa 문제가 얼마나 어려울 수 있는지를 안다 !! –

답변

7

i.add(BigInteger.ONE)은 i를 변경하지 않습니다. 대신 새로운 객체를 반환합니다. 값을 i에 할당하여 원하는 효과를 얻습니다. 코드에서 다른 유사한 호출에 대해서도 마찬가지입니다.

관련 문제