编译器错误:从 long 到 int___Fibonacci 可能有损转换

发布于 2025-01-16 08:38:35 字数 1165 浏览 0 评论 0原文

这是我在这里发表的第一篇文章,所以如果我犯了一些错误,我很抱歉。另外,编码不一定是我的专长,我正在努力掌握它并尽我所能。 所以基本上,我必须使用动态规划来解决这个问题:

Triponacci 是一个序列,其中第 n 个值等于前 3 个值的总和。我们系列的初始 3 个值(基值)是 {0, 1, 2}。请注意,我们系列中的第一个值是第 0 个值。 输出将采用语句的形式: Triponacci(2) = 2 括号中的值为输入值 n。等号右边的数字是级数中第 n 个元素的值。 Triponacci(0) = 0 Triponacci(3) = 3

我想,简单的斐波那契加上额外的一步就可以了,对吧?嗯...这就是我所做的:

static long[] storage;

public static long trip(int n)
{
    if(n<=2)
        return n;
    if(storage[n]<0)
        return storage[n];
    long result= trip(n-1) + trip(n-2)+trip(n-3);
    storage[n]= result;
    return result;
}

public static void main(String[]args)
{
    Scanner scan= new Scanner(System.in);
    long n = scan.nextLong();
    storage= new long[n+1];
    long res= trip(n);
    System.out.println(res);
}

起初它对我来说看起来很好,但是当我编译时它向我抛出了多个错误。

Triponacci.java:22: error: incompatible types: possible lossy conversion from long to int
storage= new long\[n+1\];
^
Triponacci.java:23: error: incompatible types: possible lossy conversion from long to int
long res= trip(n);
^

我应该做什么才能让它发挥作用?预先感谢您的时间和答复。

由于边界问题,我认为应该使用 long 而不是 int 。 预计工作正常但很好。

This is my first post here so if I made some mistakes I am sorry. Also, coding is not necessarily my thing, I am trying to get the hang of it and doing my best.
So basically, I have to solve this problem using dynamic programming:

Triponacci is a series where the nth value is equal to the sum of the previous 3 values. The initial 3 values (base values) of our series are {0, 1, 2}.Note that the very first value in our series is 0th value.
Output will be in the form of a statement: Triponacci(2) = 2 The value in the parenthesis is the input value n. The number to the right of the equals sign is the value of the nth element of the series.
Triponacci(0) = 0 Triponacci(3) = 3

I thought, ok easy peasy Fibonacci with an extra step, right? Well... this is what I did:

static long[] storage;

public static long trip(int n)
{
    if(n<=2)
        return n;
    if(storage[n]<0)
        return storage[n];
    long result= trip(n-1) + trip(n-2)+trip(n-3);
    storage[n]= result;
    return result;
}

public static void main(String[]args)
{
    Scanner scan= new Scanner(System.in);
    long n = scan.nextLong();
    storage= new long[n+1];
    long res= trip(n);
    System.out.println(res);
}

At first it looked fine to me but when I compiled it threw multiple errors at me.

Triponacci.java:22: error: incompatible types: possible lossy conversion from long to int
storage= new long\[n+1\];
^
Triponacci.java:23: error: incompatible types: possible lossy conversion from long to int
long res= trip(n);
^

What should I do to make it work? Thank you in advance for your time and answers.

I thought I should use long instead of int due to boundaries issues.
expected to work fine but well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末染殇 2025-01-23 08:38:35

您要求的是一个long,它是一种原始类型,可以容纳比整数更大的数字。如果您的意图实际上是允许用户输入“Triponacci(4000000000123451)” - 那么您就会遇到一个更大的问题,它太大了并且需要 BigInteger 并且可能比这更好的算法。使用 long 是有意义的,但仅限于输出(总和 - 存储数组的值)。 NOT 对于输入。

请注意,在 java 数组中必须具有 int 索引,因此从这个意义上说,如果您确实希望用户能够输入一个超出 int 范围的数字(大约为:正 20 亿到负 20 亿)。

注意:您的代码有一个错误;如果storage[n]小于小于0?我认为你的意思是大于 0。

static long[] storage;
public static long trip(int n) {
  if (n <= 2) return n;
  if (storage[n] != 0) return storage[n];
  return storage[n] = trip(n-1) + trip(n-2) + trip(n-3);
}

public static void main(String[]args) {
  Scanner scan = new Scanner(System.in);
  int n = scan.nextInt();
  storage = new long[n+1];
  long res = trip(n);
  System.out.println(res);
}

You're asking for a long which is a primitive type that can hold larger numbers than ints. If your intent is to actually allow the user to enter 'Triponacci(4000000000123451)' - then you have a much bigger problem, that's way too large and requires BigInteger and probably a better algorithm than this. Using long can make sense, but only for the outputs (the sums - the value of the storage array). NOT for the inputs.

Note that in java arrays must have int indices, so in that sense, you need a much more complicated algorithm in any case if you really intend for the user to be able to enter a number beyond the confines of int (which are: plus 2 billion to minus 2 billion, approximately).

NB: Your code has a bug; if storage[n] is less than 0? I think you meant more than 0.

static long[] storage;
public static long trip(int n) {
  if (n <= 2) return n;
  if (storage[n] != 0) return storage[n];
  return storage[n] = trip(n-1) + trip(n-2) + trip(n-3);
}

public static void main(String[]args) {
  Scanner scan = new Scanner(System.in);
  int n = scan.nextInt();
  storage = new long[n+1];
  long res = trip(n);
  System.out.println(res);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文