#include <iostream>
using namespace std;
int main()
{
int num1 = 0;
int num2 = 1;
int num_temp;
int num_next = 1;
int n;
cin >> n;
for (int i = 0; i < n; i++){
cout << num_next << " ";
num_next = num1 + num2;
num1 = num2;
num_temp = num2;
num2 = num_next - num1;
num1 = num_temp;
}
return 0;
}
我必须输出第一个“n”斐波纳契数,但我认为逻辑上存在一些问题。我无法找出我做错了什么。前3或4个元素是正确的,但后来出现问题...
预期:
对于n=9
0,1,1,2,3,5,8,13,21
实际值:
1 1 1 1 1 1 1 1 1
答案 0 :(得分:5)
#include <iostream>
using namespace std;
int main()
{
int num1 = 0;
int num2 = 1;
int num_temp;
int num_next = 1;
int n;
cin >> n;
if (n>=1)
cout << 0 << " ";
if (n>=2)
cout << 1 << " ";
for (int i = 0; i < n-2; i++){
num_next = num1 + num2;
cout << num_next << " ";
num1 = num2;
num2 = num_next;
}
cout << endl;
return 0;
}
答案 1 :(得分:3)
试试这个。这有点不同,但会让你在那里一样。
#include <iostream>
using namespace std;
int main()
{
int input(0), Alpha(0), Beta(1), Total(1);
cout << "Please input a top number: ";
cin >> input;
for(int i = 0; i <= input; i++)
{
cout << Total << endl;
Total = Alpha + Beta;
Alpha = Beta;
Beta = Total;
}
}
答案 2 :(得分:2)
Fibonacci序列为{0,1,1,2,3,... N - 1,N,2N - 1}。
要实现它,您需要拥有N - 2
变量和N - 1
变量,以便计算N = (N - 2) + (N - 1)
:
unsigned int count = 0;
std::cin >> count;
// assume count >= 2
unsigned int prev2 = 0;
unsigned int prev1 = 1;
std::cout << prev2 << " " << prev1 << " ";
for (unsigned int i = 2; i < count; ++i)
{
unsigned int current = prev2 + prev1;
prev2 = prev1;
std::cout << current << " ";
prev1 = current;
}
std::cout << std::endl;
答案 3 :(得分:2)
这是我的版本。
它与之前的样本大致相同,但我想展示使用环形缓冲区。
// Study for algorithm that counts n:th fibonacci number
// Fibonacci[1] == 1 and Fibonacci[2] == 1 (and Fibonacci[0] == 0)
// Fibonacci[n] = Fibonacci[n-1] + Fibonacci[n-2]
#include <cstdio>
#include <iostream>
#include <cstdlib>
int main(int argc, const char* argv[])
{
// not counting trivial Fibonacci[0]
if(argc != 2 || atoi(argv[1]) < 1){
std::cout << "You must provide one argument. Integer > 0" << std::endl;
return EXIT_SUCCESS;
}
// ring buffer to store previous two fibonacci numbers, index it with [i%2]
// seeded with Fibonacci[1] and Fibonacci[2]
// if you want to count really big fibonacci numbers, you have to make your own type for
// buffer variable
// this type overflows after [93] with my macbook
unsigned long long int buffer[2]={ 1, 1 };
// n:th Fibonacci
unsigned int fn = atoi(argv[1]);
// count loop is used if seeked fibonacci number is gt 2
if(fn > 2){
for(unsigned int i = 2; i < fn; ++i){
buffer[i%2] = buffer[(i-1)%2] + buffer[(i-2)%2];
}
}
// Result will be send to cout
std::cout << "Fibonacci[" << fn << "] is " << buffer[(fn-1)%2] << std::endl;
return EXIT_SUCCESS;
}
答案 4 :(得分:1)
#include <iostream>
using std::cout; using std::cin;
int main()
{
unsigned int a=0u, b=1u, n;//assuming n is a positive number.
//otherwise make it int instead of unsigned
//and check if it's negative
cin >> n;
if(n==0)
{return 0;}
if(n==1)
{cout << a; return 0;}
cout << a << " " << b << " ";
for(unsigned int i=2u ; i<n; i++)
{
b = a + b;
a = b - a;
cout << b << " ";
}
return 0;
}
通过将最后两个值加在一起,它将下一个值放在'b'中。 'a'然后得到前一个b值。假设a = 3且b = 5.然后新b将变为8,'a'将变为5.这是因为它总是将最后两个数相加以得到下一个数的结果。下一个操作将总和5(当前a)和8(当前b)等等......
答案 5 :(得分:1)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[50],n,i;
cout<<"Enter the no. of elements to be printed in fibonacci series : ";
cin>>n;
arr[0]=0;arr[1]=1;
for(i=2;i<n;i++)
{
arr[i]=arr[i-1]+arr[i-2];
}
cout<<"\nThe fibonacii series is : "<<endl;
for(i=0;i<n;i++)
{
cout<<arr[i]<<"\t";
}
getch();
}
答案 6 :(得分:1)
堆栈溢出当然是递归版本的限制。如果这不是问题,您可能还需要考虑递归Fibonacci的模板化版本。
#include <iostream>
template <int N> int Fib(){ return Fib<N-1>() + Fib<N-2>(); }
template <> int Fib<1>() { return 1; }
template <> int Fib<0>() { return 1; }
using namespace std;
int main()
{
// For the 10th Fibbonacci number...
cout << Fib<10>() << endl;
return 0;
}
答案 7 :(得分:0)
我一直在寻找一些递归解决方案来执行相同的任务,大多数人做的是,他们编写一个递归函数来查找第n个Fibonacci数,然后在主程序中,它们运行一次循环n次,并且使用值1到n调用此递归函数来获取所有n个Fibonacci数并打印它们,这是一个很大的开销。
这是一个执行相同任务的解决方案,但它只调用递归函数一次以获取所有n个Fibonacci数,并将它们存储在数组中,然后打印。这是((n-1)*(递归调用的开销))比我前面提到的快一倍。如果您发现它有帮助,请竖起大拇指:)
#include<iostream>
using namespace std;
int *arr;
int iter = 0;
int len;
int returnValue;
void exist(int num, int arr[] ) /* this function checks if the Fibonacci number that recursive function have calcuated is already in the array or not, mean if it is already calculated by some other recursive call*/
{
bool checkExistance = false; /* if this is true, means this Fibonacci number is already calculated and saved in array, so do not save it again*/
returnValue = num;
for (int i = 0; i< len; i++)
{
if(arr[i]==num)
{
checkExistance = true;
break;
}
}
if(!checkExistance)
{
arr[iter]=num;
iter++;
}
}
int fibonacci(int n)
{
if (n==1)
{
exist(1,arr);
return 1;
}
else if (n==2)
{
exist(1,arr);
return 1;
}
else
{
exist((fibonacci(n-1)+fibonacci(n-2)),arr);
return returnValue;
}
}
int main()
{
int n;
cout<<"Enter the number of Fibonacci you want to print: ";
cin>>n;
len = n;
arr = new int[n];
fibonacci(n);
arr[n-1] = 1;
cout<<"1:\t"<<arr[n-1]<<endl;
for (int i = 0; i< len-1; i++)
{
cout<<i+2<<":\t"<<arr[i]<<endl;
}
return 0;
}
答案 8 :(得分:0)
#include <iostream>
using namespace std;
int main()
{
int num1 = 0, num2 = 1 , num_next = 1, n;
cout << "enter a number: \n";
cin >> n;
//for when a negative value is given
while(n < 0)
{
cout << "ERROR\n";
cin >> n;
}
//when any positive number (above 1 is given)
if (n > 0)
{
//to give the value of 0 without ruining the loop
cout << num1 << " ";
for (int i = 0; i < n; i++)
{
//the Fibonacci loop
cout << num_next << " ";
num_next = num1 + num2;
num1 = num2;
num2 = num_next;
}
}
//for when 0 is the given value
else if (n == 0)
cout << n << " ";
return 0;
}
答案 9 :(得分:0)
这是一个没有临时变量的解决方案:
#include <iostream>
using namespace std;
int main()
{
int n0 = 0;
int n1 = 1;
int n;
cout << "Prints first N in Fibonacci series. Please enter a number for N: ";
cin >> n;
for(int i = 0; i < n; i++) {
cout << n0 << " ";
n1 = n0 + n1;
n0 = n1 - n0;
}
}
也是一个(尾部)递归解决方案:
#include <iostream>
using namespace std;
void fib(int n, int n0, int n1) {
if(n <= 0) {
return;
} else {
cout << n0 << " ";
fib(n-1, n1, n0 + n1);
}
}
int main()
{
int n;
cout << "Prints first N in Fibonacci series. Please enter a number for N: ";
cin >> n;
fib(n, 0, 1);
}
答案 10 :(得分:0)
/* Author: Eric Gitangu
Date: 07/29/2015
This program spits out the fibionacci sequence for the range of 32-bit numbers
Assumption: all values are +ve ; unsigned int works here
*/
#include <iostream>
#include <math.h>
#define N pow(2.0,31.0)
using namespace std;
void fibionacci(unsigned int &fib, unsigned int &prevfib){
int temp = prevfib;
prevfib = fib;
fib += temp;
}
void main(){
int count = 0;
unsigned int fib = 0u, prev = 1u;
while(fib < N){
if( fib ==0 ){
fib = 0;
cout<<" "<< fib++ <<" \n ";
continue;
}
if( fib == 1 && count++ < 2 ){
fib = 1;
cout<< fib <<" \n ";
continue;
}
fibionacci(fib, prev);
cout<< fib <<" \n ";
}
}
答案 11 :(得分:0)
所以...这是一个解决方案,“如果你想要一个特定的Fib序列。”
int max = a[0];
for(int i = 0; i < n; ++i)
{
if(max < a[i]) {
max = a[i];
}
}
答案 12 :(得分:0)
如何看待递归解决方案:
void fibonacci(int n1, int n2, int numCount)
{
--numCount;
if (numCount > 0)
{
cout << n1 << ", ";
fibonacci(n2, n1 + n2, numCount);
}
else
cout << n1 << endl;
}
然后你可以称之为:
enterint fNum;
cout << "Enter a non negative number to print output fibonacci sequence: ";
cin >> fNum;
fibonacci(0, 1, fNum);
输出示例:
答案 13 :(得分:0)
简洁版本:
int n, a{0}, b{1};
std::cin >> n;
while (n-- > 0) {
std::cout << a << " ";
std::tie(a, b) = std::make_tuple(b, a + b);
}
答案 14 :(得分:0)
您可以编写代码来生成斐波那契数列,从而避免if-else语句输出零和一,避免在循环外打印它们,并避免使用'temp'整数。您可以通过用-1和1初始化'first'和'second'变量来做到这一点,因此它们之间的总和将为0,这是该系列的第一个器官,而循环将完成其余的工作
#include <iostream>
using namespace std;
int main()
{
int num, a = -1, b = 1;
cout << "enter a number:" << endl;
cin >> num;
for (int i = 0 ; i <= num ; i++ )
{
b += a;
cout << b << " ";
a = b - a;
}
cout << endl;
return 0;
}