如何打印出斐波那契数列的相加结果?

huangapple go评论64阅读模式
英文:

How to print out the addition of the fibonacci sequence?

问题

我在互联网上看到了这段代码,决定尝试一下,但我一直在想,如何打印出“斐波那契”的相加结果?

```java
package fibonacci;
import java.util.Scanner;

public class Fibonacci {

    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int k, n, a = 1, b = 1;
        
        k = 0;
        
        System.out.println("输入数字:");
        n = sc.nextInt();
        
        System.out.print("0 1 1 ");
        
        while (k <= n) {
            k = a + b;
            
            if (k >= n) break;
            System.out.print(k + " " );
            a = b;
            b = k;
        }
        
        System.out.println("0 + 1 = 1");
        System.out.println("1 + " + a + " = " + b);
    }
}

你如何生成类似这样的输出:

0 1 1 2 3 5 8
0 + 1 = 1
1 + 1 = 2
1 + 2 = 3
2 + 3 = 5
3 + 5 = 8

<details>
<summary>英文:</summary>

I saw this code on the internet and decided to try it myself, but I&#39;ve been wondering, how do you print out the addition of the &quot;fibonacci&quot;?

package fibonacci;
import java.util.Scanner;

public class Fibonacci {

public static void main(String[] args) {
    
    Scanner sc = new Scanner (System.in);
    
    int k, n, a = 1, b = 1;
    
    k = 0;
    
    System.out.println(&quot;input number: &quot;);
    n = sc.nextInt();
    
    System.out.print(&quot;0 1 1 &quot;);
    
    while (k &lt;= n) {
        k = a + b;
        
        if (k &gt;= n) break;
        System.out.print(k + &quot; &quot; );
        a = b;
        b = k;
    }
    
    System.out.println(&quot;Sum of 0 + 1 = 1&quot;);
    System.out.println(&quot;Sum of 1 +&quot; + a + &quot; = &quot; + b);
}

}


How can you generate an output like this:

    0 1 1 2 3 5 8
    0 + 1 = 1
    1 + 1 = 2
    1 + 2 = 3
    2 + 3 = 5
    3 + 5 = 8

</details>


# 答案1
**得分**: 0

开始一个字符串,初始计算为 (0 + 1 = 1),然后在每次循环迭代中将当前计算附加到其中,即:

```java
System.out.print("0 1 1 ");
String addition = "0 + 1 = 1\n";
while (k <= n) {
    k = a + b;
    addition += a + " + " + b + " = " + k + "\n";
    if (k >= n) break;
    System.out.print(k + " " );
    a = b;
    b = k;
}
System.out.println();
System.out.println(addition);
英文:

Start a string with the initial calculation (0 + 1 = 1) and then append to it in each iteration of the loop the current calculation i.e.

System.out.print(&quot;0 1 1 &quot;);
String addition = &quot;0 + 1 = 1\n&quot;;
while (k &lt;= n) {
    k = a + b;
    addition += a+ &quot; + &quot; +b + &quot; = &quot; + k + &quot;\n&quot;;
    if (k &gt;= n) break;
    System.out.print(k + &quot; &quot; );
    a = b;
    b = k;
}
System.out.println();
System.out.println(addition);

答案2

得分: 0

这应该能回答你的问题

package com.example.demo;

import java.util.Scanner;

public class Fibonaccci {
  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    int k, n, a = 0, b = 1;

    k = 0;

    System.out.println("输入数字:");
    n = sc.nextInt();

    System.out.print("0 1 ");
    StringBuffer acumResults = new StringBuffer("\n");
    while (k <= n) {
      k = a + b;
      acumResults.append(a + " + " + b + " = " + k + "\n");
      System.out.print(k + " ");
      if (k >= n) break;
      a = b;
      b = k;
    }
    System.out.println(acumResults);

  }
}
英文:

this should answer your question:

package com.example.demo;

import java.util.Scanner;

public class Fibonaccci {
  public static void main(String[] args) {

    Scanner sc = new Scanner (System.in);

    int k, n, a = 0, b = 1;

    k = 0;

    System.out.println(&quot;input number: &quot;);
    n = sc.nextInt();

    System.out.print(&quot;0 1 &quot;);
    StringBuffer acumResults= new StringBuffer(&quot;\n&quot;);
    while (k &lt;= n) {
      k = a + b;
      acumResults.append(a+&quot; + &quot;+b+&quot; = &quot;+k+&quot;\n&quot;);
      System.out.print(k + &quot; &quot; );
      if (k &gt;= n) break;
      a = b;
      b = k;
    }
    System.out.println(acumResults);

  }
}

答案3

得分: 0

要产生与您的输出完全相同的结果,我会编写以下代码:

package fibonacci;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

public class Fibonacci {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int k, n, a = 1, b = 1;

        k = 0;

        List<Integer> numbers = new ArrayList<Integer>();

        System.out.println("输入数字:");
        n = sc.nextInt();

        System.out.print("0 1 1 ");
        numbers.add(1);
        numbers.add(1);

        while (k <= n) {
            k = a + b;

            if (k >= n) break;
            System.out.print(k + " ");
            a = b;
            b = k;
            numbers.add(k);
        }

        // 由于我不知道您的Java版本,所以使用了for循环
        System.out.println();
        int oldSum = 0;
        for (int i = 0; i < numbers.size(); ++i) {
            int element = numbers.get(i);
            System.out.println(oldSum + " + " + element + " = " + (oldSum + element));
            oldSum += element;
        }
    }
}

请注意,代码中的变量名和语法保持不变。如果您对代码进行了更改,可能需要适应您的Java版本或其他需求。

英文:

To produce exactly your output I would code the following:

package fibonacci;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int k, n, a = 1, b = 1;
k = 0;
List&lt;Integer&gt; numbers = new ArrayList&lt;Integer&gt;();
System.out.println(&quot;input number: &quot;);
n = sc.nextInt();
System.out.print(&quot;0 1 1 &quot;);
numbers.add(1);
numbers.add(1);
while (k &lt;= n) {
k = a + b;
if (k &gt;= n) break;
System.out.print(k + &quot; &quot; );
a = b;
b = k;
numbers.add(k);
}
// used for loop since I don&#39;t know your Java version
System.out.println();
int oldSum = 0;
for (int i= 0; i &lt; numbers.size(); ++i) {
int element = numbers.get(k);
System.out.println oldSum + &quot; + &quot; + element + &quot; = &quot; + (oldSum + element);
oldSum += element;
}
}
}

huangapple
  • 本文由 发表于 2020年10月20日 17:34:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64442394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定