do-while 斐波那契数列重复答案
do-while in fibonacci sequence repeating answer
我做了一个关于斐波那契数列的程序。我想重复这个程序,所以我使用了 do-while 循环。但是,似乎上一个结果的最后两个数字不断出现。它应该重置回第一个任期。请帮助我如何到达那里。
#include<iostream>
using namespace std;
void printFibonacci(int n){
static int n1=1, n2=1, n3=0;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
cout<<n3<<" ";
printFibonacci(n);
}
}
int main(){
int n;
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Fibonacci Series: ";
cout<<"0 "<<"1 ";
printFibonacci(n-2); //n-2 because 2 numbers are already printed
return 0;
}
你的代码的问题是你没有在循环结束后重置你的变量。因此,要解决此问题,只需在 do-while
循环中定义您的变量:
do {
int t1 = 1, t2 = 1, nextTerm = 0;
..或在 1 个循环完成后重置您的变量:
else {
cout << "Thank you for using the program." << endl;
}
t1 = 1, t2 = 1, nextTerm = 0;
但是 n
必须在循环外定义(正如您所做的那样):
std::cout << "Fibonacci Program" << std::endl;
int n;
do {
也不需要创建变量i
:
int /*i,*/ t1 = 1, t2 = 1, nextTerm = 0;
..因为您稍后在此处的 for 循环中创建了它:
for (int i = 1; i <= n; ++i) {
此外,请考虑不要在您的代码中使用以下内容:
using namespace std;
..因为它被认为是一种不好的做法。有关这方面的更多信息,请查看 why is using namespace std
considered a bad practice.
当您第一次运行编写代码时,变量 t1、t2 和 nextTerm 的值正在发生变化。因此,在再次重复相同的代码之前,您需要再次设置这些变量的默认值。
简单地试试这个:
#include <iostream>
using namespace std;
int main (){
int i, n, t1=1, t2=1, nextTerm=0;
cout << "Fibonacci Program" << endl;
do{
t1=1;
t2=2;
nextTerm=0;
cout << "How many elements? ";
cin >> n;
if(n>=1){
cout << "Sequence: ";
for (int i = 1; i <= n; ++i){
if(i == 1) {
cout << t1 << " ";
continue;
}
if(i == 2) {
cout << t2 << " ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << " ";
}
cout << endl;
}
else{
cout << "Thank you for using the program." << endl;
}
}
while(n>=1);
return 0;
}
我做了一个关于斐波那契数列的程序。我想重复这个程序,所以我使用了 do-while 循环。但是,似乎上一个结果的最后两个数字不断出现。它应该重置回第一个任期。请帮助我如何到达那里。
#include<iostream>
using namespace std;
void printFibonacci(int n){
static int n1=1, n2=1, n3=0;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
cout<<n3<<" ";
printFibonacci(n);
}
}
int main(){
int n;
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Fibonacci Series: ";
cout<<"0 "<<"1 ";
printFibonacci(n-2); //n-2 because 2 numbers are already printed
return 0;
}
你的代码的问题是你没有在循环结束后重置你的变量。因此,要解决此问题,只需在 do-while
循环中定义您的变量:
do {
int t1 = 1, t2 = 1, nextTerm = 0;
..或在 1 个循环完成后重置您的变量:
else {
cout << "Thank you for using the program." << endl;
}
t1 = 1, t2 = 1, nextTerm = 0;
但是 n
必须在循环外定义(正如您所做的那样):
std::cout << "Fibonacci Program" << std::endl;
int n;
do {
也不需要创建变量i
:
int /*i,*/ t1 = 1, t2 = 1, nextTerm = 0;
..因为您稍后在此处的 for 循环中创建了它:
for (int i = 1; i <= n; ++i) {
此外,请考虑不要在您的代码中使用以下内容:
using namespace std;
..因为它被认为是一种不好的做法。有关这方面的更多信息,请查看 why is using namespace std
considered a bad practice.
当您第一次运行编写代码时,变量 t1、t2 和 nextTerm 的值正在发生变化。因此,在再次重复相同的代码之前,您需要再次设置这些变量的默认值。 简单地试试这个:
#include <iostream>
using namespace std;
int main (){
int i, n, t1=1, t2=1, nextTerm=0;
cout << "Fibonacci Program" << endl;
do{
t1=1;
t2=2;
nextTerm=0;
cout << "How many elements? ";
cin >> n;
if(n>=1){
cout << "Sequence: ";
for (int i = 1; i <= n; ++i){
if(i == 1) {
cout << t1 << " ";
continue;
}
if(i == 2) {
cout << t2 << " ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << " ";
}
cout << endl;
}
else{
cout << "Thank you for using the program." << endl;
}
}
while(n>=1);
return 0;
}