如何在我的代码中实现BigInteger类?

时间:2015-08-11 01:08:37

标签: java biginteger fibonacci

我正在开发一个程序,该程序应该沿Fibonacci序列返回给定数字的位置。

很简单,但Codeabbey上的测试用例长度超过100位。这不仅仅是长原始数据类型可以处理的。我知道我需要使用BigInteger,但我不知道如何在我的代码中实现它。我读到BigInteger是不可变的?这是什么意思?

这是我的代码:

import java.util.Scanner;
class codeabbey67
{
    public static void main(String[] Args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Sets: ");
        int sets = input.nextInt();
        long A[] = new long[sets];
        for(int i = 0; i<sets; i++)
        {
            long f = 0;
            long s = 1;
            long next = 0;
            long j = 0;
            System.out.print("\nVal: ");
            long val = input.nextLong();
            while(next != val)
            {
                if(j<= 1)
                {
                    next = 1;
                    j++;
                }
                next = f+s;
                f = s;
                s = next;
                j++;
            }
            A[i] = j;
        }
        System.out.println("\nRESULTS: ");
        for(int j = 0; j<A.length; j++)
            System.out.print(A[j] + " ");
    }
}

编辑: 这是我使用BigInteger的更新代码。仍然没有运气。

import java.util.Scanner;
import java.math.BigInteger;

class codeabbey67
{
    public static void main(String[] Args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("\n\nSets: ");
        int sets = input.nextInt();
        int A[] = new int[sets];
        for(int i = 0; i<sets; i++)
        {
            BigInteger f = BigInteger.ZERO;
            BigInteger s = BigInteger.ONE;
            BigInteger next = BigInteger.ZERO;
            BigInteger j = BigInteger.ZERO;
            System.out.print("\nVAL: ");
            BigInteger val = input.nextBigInteger();
            int x = 0;
            while(!next.equals(val) && x!= 1000) //until current value at position in sequence equals desired value 
            {
                if(x<= 1)
                {
                    next = BigInteger.ONE;
                    x++;
                }
                next = f.add(s);
                s=next;
                x++;
            }
            A[i] = x;
        }
        for(int y = 0; y<A.length; y++)
            System.out.print(A[y] + " ");
    }
}
编辑:想出来。感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

BigInteger附带了可用于修改存储在其中的数值的方法。 This可能有助于了解如何使用BigInteger。

不可变意味着您无法修改现有对象,您只能创建一个新对象。想象一下像java.awt.Color这样的类:该类的任何字段都不可编辑,因此它是不可变的。另一个例子是String类。

因为BigInteger方法操作例如在所述操作之后,add(),subtract()等都返回包含新值的BigInteger对象,您可以将现有BigInteger引用变量重新分配给由此操作返回的BigInteger对象:

BigInteger sum = new BigInteger ("0", 10);
sum = sum.add (new BigInteger ("123", 10)); //sum’s value is now 123

在你的情况下,由于你已经使用了long,你可以使用BigInteger方法valueOf(),它接受一个long参数并返回一个由long值组成的BigInteger对象。 E.g。

BigInteger sum = BigInteger.valueOf (123);//sum’s value is now set to 123

答案 1 :(得分:0)

就像@dabigone所说,你可以使用BigInteger而不是自己实现它,我尝试使用BigInteger为这个Codeabbey67更改代码:

public class Codeabbey67 {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int N = in.nextInt();
    while (N-- > 0) {
        BigInteger bi = new BigInteger(in.next());
        BigInteger i = new BigInteger("0");
        BigInteger j = new BigInteger("1");
        int idx = 0;
        while (!i.equals(bi) && idx <= 1000) {
            j = i.add(j);
            i = j.subtract(i);
            idx++;
        }
        System.out.print(idx + " ");
    }
}
相关问题