2012-04-30 3 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)'はノーオペレーションです。代わりに 'i = i.add(BigInteger.ONE)'を使用し、他の場所でも同じことをします。 –

+2

for forループ?何も見えません。 – mata

+0

さらに、このアルゴリズムを使用して「受け入れられる」とは思わないかもしれません。**素因数分解**を使用することを考慮してください**、キーワードを使用して検索してください:** 'Number Theory' **、**'カウント除数 '**。あなたはおそらく** TimeLimitExceeded **を取得しようとしています。私はいくつかのUVaの問題がいかに難しいか知っています! –

答えて

7

i.add(BigInteger.ONE) iを変更しない。代わりに新しいオブジェクトを返します。値をiに割り当て、必要な効果を得ます。あなたのコード内の他の同様の呼び出しについても同じことが言えます。

関連する問題