英文:
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've been wondering, how do you print out the addition of the "fibonacci"?
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("input number: ");
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("Sum of 0 + 1 = 1");
System.out.println("Sum of 1 +" + a + " = " + 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("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);
答案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("input number: ");
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);
}
}
答案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<Integer> numbers = new ArrayList<Integer>();
System.out.println("input number: ");
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);
}
// used for loop since I don't know your Java version
System.out.println();
int oldSum = 0;
for (int i= 0; i < numbers.size(); ++i) {
int element = numbers.get(k);
System.out.println oldSum + " + " + element + " = " + (oldSum + element);
oldSum += element;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论