Java 如何在代码中实现BigInteger类?

Java 如何在代码中实现BigInteger类?,java,biginteger,fibonacci,Java,Biginteger,Fibonacci,我正在编写一个程序,该程序应该返回给定数字沿斐波那契序列的位置 很简单,但是Codeabbey上的测试用例长度超过100位。这超出了长基元数据类型所能处理的范围。我知道我需要使用BigInteger,但我不确定如何在代码中实现它。我读到BigInteger是不可变的?这是什么意思 这是我的密码: import java.util.Scanner; class codeabbey67 { public static void main(String[] Args) {

我正在编写一个程序,该程序应该返回给定数字沿斐波那契序列的位置

很简单,但是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] + " ");
    }
}
import java.util.Scanner;
类别代码abbey67
{
公共静态void main(字符串[]Args)
{
扫描仪输入=新扫描仪(System.in);
系统输出打印(“集合:”);
int set=input.nextInt();
长A[]=新长[套];
for(int i=0;i提供了一些方法,可以用来修改存储在其中的数值。学习如何使用BigInteger可能很有用

Immutable意味着您不能修改现有对象,只能创建一个新对象。想想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,因此可以使用biginger方法valueOf(),该方法接受long参数并返回由long值组成的biginger对象

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

正如@dabigone所说,您可以使用BigInteger而不是自己实现它,我尝试使用BigInteger对代码Abbey67进行更改,如下所示:

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 + " ");
    }
}
公共类代码abbey67{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
int N=in.nextInt();
而(N-->0){
BigInteger bi=新的BigInteger(in.next());
BigInteger i=新的BigInteger(“0”);
BigInteger j=新的BigInteger(“1”);
int-idx=0;

虽然(!i.equals(bi)&&idx@NolanHamilton,但实际上没有办法使用biginger方法来执行操作,因此在这里您可以做的不多。另外,您可以使用compareTo()方法来比较BigInteger对象。如果您还没有这样做,请提一下。我知道。无论如何,谢谢。谢谢。出于好奇,为什么测试用例会这么长?我想不出为什么它们不能只有5-10位。@NolanHamilton似乎没有必要输出“\n\n集:”和“\nVAL:”根据我对接受结果的理解。而且,似乎不需要用数组存储每个索引,只需按空格分隔逐个打印即可。
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 + " ");
    }
}