【问题标题】:How can I implement the BigInteger class into my code?如何在我的代码中实现 BigInteger 类?
【发布时间】:2015-08-11 01:08:36
【问题描述】:

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

很简单,但 Codeabbey 上的测试用例长度超过 100 位。这超出了 long 原始数据类型可以处理的范围。我知道我需要使用 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] + " ");
    }
}

编辑:想通了。感谢所有的帮助!

【问题讨论】:

    标签: java biginteger fibonacci


    【解决方案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 对象。例如

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

    【讨论】:

    • @NolanHamilton 实际上没有办法使用 BigInteger 方法来执行操作,因此您无能为力。另外,您可以使用 BigInteger 类中的 compareTo () 方法来比较 BigInteger 对象。只是提一下,以防您还没有这样做。
    • 我知道。无论如何,谢谢。
    【解决方案2】:

    就像@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 + " ");
        }
    }
    

    【讨论】:

    • 谢谢。出于好奇,为什么测试用例会这么长?我想不出为什么它们不能只是 5-10 位数。
    • @NolanHamilton 似乎不需要输出“\n\nSets:”和“\nVAL:”作为我对 Accepted 结果的理解。另外,似乎不需要用数组存储每个索引,只需将它们一个一个打印出来,用空格分隔即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    相关资源
    最近更新 更多