【问题标题】:Correct usage of long primitive datatype and its array正确使用长原始数据类型及其数组
【发布时间】:2016-12-20 12:47:29
【问题描述】:

我一直在编写一个问题,要求我 code a modified version of fibonacci.
现在虽然代码已经准备就绪,但它不能正常工作,因为返回的是 int 而不是 long

当以下用作输入时:0 1 10 输出是-9223372036854775807 而不是这个84266613096281243382112

代码如下:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    static long arr[];

    public static long func(int n,long t1, long t2){
    arr = new long[n+1];
    arr[1]=t1;
    arr[2]=t2;
    return func(arr,n);    

    }
    public static long func(long[] arr, int n){
        if(n==0||n==1||n==2){
            return arr[n];
        }
        if(arr[n]!=0){
            return arr[n];
        }
        arr[n]=(long)(Math.pow(func(arr,n-1),2)+func(arr,n-2));
        return arr[n];
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        long t1= s.nextLong();
        long t2=s.nextLong();
        int n=s.nextInt();
        System.out.print(func(n,t1,t2));
    }
}


我不是在寻求逻辑方面的帮助,也不是在解决问题。我想知道我哪里出错了,即使我使用 long 数组,返回的答案仍然是一个 int

此外,关于如何处理长数组的任何帮助都会很棒。

【问题讨论】:

  • 为什么要将long 转换为int
  • 更仔细地查看您的代码,我看不到您将 long 转换为 int 的位置。为什么说“因为 long 到 int 的转换错误”?
  • 您在哪里看到了转化?
  • 我对您说的“因为将 long 转换为 int 错误”的评论感到困惑。正如你所说,你的代码中没有这样的转换,所以这不是问题。
  • 我建议您查看pow() 的文档。它返回一个double

标签: java arrays long-integer type-conversion fibonacci


【解决方案1】:

返回的答案是not integer,因为整数的最大值为2147483647,如果值大于integer,则返回[-2147483647, 2147483647] 之间的值。我觉得逻辑有问题。

【讨论】:

  • 但是我的代码通过了 2 个测试用例,这两个测试用例都在 int 的范围内:
  • 测试用例1:2 1 7 测试用例2:0 1 5
  • 遇到了问题。实际上你输入的是双精度值而不是整数
  • System.out.println((long)Double.MAX_VALUE); 的最大值是 9223372036854775807,并且您的答案超过了 Math.pow() 中返回 double value 的 double 值。
  • 您能否更具体地说明该语句?我应该对哪个整数值进行类型转换?
【解决方案2】:

对于所有想知道的人,我昨天意识到 long 的范围已用尽,因此无法用于解决问题。我使用了 BigInteger 类,它可以工作。

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {


    public static BigInteger func(int n,BigInteger t1, BigInteger t2){
    BigInteger arr[] = new BigInteger[n+1];
    arr[1]=t1;
    arr[2]=t2;
    return func(arr,n);    

    }
    public static BigInteger func(BigInteger[] arr, int n){
        if(n==0||n==1||n==2){
            return arr[n];
        }
        if(arr[n]!=null){
            return arr[n];
        }
        arr[n]=(func(arr,n-1).pow(2)).add(func(arr,n-2));
        return arr[n];
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        BigInteger t1= s.nextBigInteger();       
        BigInteger t2= s.nextBigInteger();       
        int n=s.nextInt();
        System.out.print(func(n,t1,t2));
    }
}

【讨论】:

    【解决方案3】:

    您可以改用此代码,因为他们没有要求整个数组,而只要求第 n 个术语。

    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
        public static void main(String args[] ) throws Exception {
            Scanner in=new Scanner(System.in);
            BigInteger t1=in.nextBigInteger();
            BigInteger t2=in.nextBigInteger();
            int n=in.nextInt();
            for(int i=0;i<n-2;i++)
            {
                BigInteger temp;
                temp = (t2.pow(2)).add(t1);
                t1=t2;
                t2=temp;
            } 
            System.out.println(t2);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      • 2016-09-01
      • 1970-01-01
      相关资源
      最近更新 更多