C++ C+中的斐波那契级数+; #包括 使用名称空间std; int main() { int num1=0; int num2=1; int num_temp; int num_next=1; int n; cin>>n; 对于(int i=0;i>n; 如果(n>=1) 试试这个吧。这有点不一样,但同样会让你达到目的 #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; } #包括 使用名称空间std; int main() { int输入(0)、Alpha(0)、Beta(1)、Total(1); cout>输入; 对于(inti=0;i
Fibonacci序列是{0,1,1,2,3,…N-1,N,2N-1} 为了实现它,您需要一个C++ C+中的斐波那契级数+; #包括 使用名称空间std; int main() { int num1=0; int num2=1; int num_temp; int num_next=1; int n; cin>>n; 对于(int i=0;i>n; 如果(n>=1) 试试这个吧。这有点不一样,但同样会让你达到目的 #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; } #包括 使用名称空间std; int main() { int输入(0)、Alpha(0)、Beta(1)、Total(1); cout>输入; 对于(inti=0;i,c++,fibonacci,C++,Fibonacci,Fibonacci序列是{0,1,1,2,3,…N-1,N,2N-1} 为了实现它,您需要一个N-2变量和一个N-1变量,以便您可以计算N=(N-2)+(N-1): 无符号整数计数=0; 标准::cin>>计数; //假设计数>=2 无符号整数prev2=0; 无符号整数prev1=1; std::cout#包括 使用std::cout;使用std::cin; int main() { 无符号整数a=0u,b=1u,n;//假设n为正数。 //否则,将其设为int而不是unsigned //检查
N-2
变量和一个N-1
变量,以便您可以计算N=(N-2)+(N-1)
:
无符号整数计数=0;
标准::cin>>计数;
//假设计数>=2
无符号整数prev2=0;
无符号整数prev1=1;
std::cout#包括
使用std::cout;使用std::cin;
int main()
{
无符号整数a=0u,b=1u,n;//假设n为正数。
//否则,将其设为int而不是unsigned
//检查是否为阴性
cin>>n;
如果(n==0)
{返回0;}
如果(n==1)
{cout#包括
#包括
void main()
{
clrsc();
int-arr[50],n,i;
coutn;
arr[0]=0;arr[1]=1;
对于(i=2;i这是我的版本
它与前面的示例大致相同,但我想展示一下环形缓冲区的使用
#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();
}
//计算第n个斐波那契数的算法研究
//斐波那契[1]==1和斐波那契[2]==1(和斐波那契[0]==0)
//斐波那契[n]=斐波那契[n-1]+斐波那契[n-2]
#包括
#包括
#包括
int main(int argc,const char*argv[]
{
//不计算平凡斐波那契[0]
if(argc!=2 | | atoi(argv[1])<1){
std::cout我一直在寻找一些递归解决方案来完成同样的任务,人们主要做的是,他们编写一个递归函数来查找第n个Fibonacci数,然后在主程序中,他们运行一个循环n次,调用这个递归函数,值为1到n,以得到所有的n个Fibonacci数并打印它们,这是一个b间接费用
这里有一个解决方案,它执行相同的任务,但它只调用递归函数一次,以获取最多n个Fibonacci数,并将它们存储在数组中,然后打印。这比我前面提到的方法快((n-1)*(递归调用的开销))倍。如果您觉得有帮助,请竖起大拇指:)
#包括
使用名称空间std;
int*arr;
int-iter=0;
内伦;
返回值;
void exist(int num,int arr[])/*此函数检查递归函数计算的斐波那契数是否已在数组中,即是否已由其他递归调用计算*/
{
bool checkExistance=false;/*如果为真,则表示此斐波那契数已计算并保存在数组中,因此不要再次保存它*/
returnValue=num;
对于(int i=0;in;
//对于给定负值的情况
而(n<0)
{
cout>n;
}
//当给出任何正数(大于1)时
如果(n>0)
{
//在不破坏循环的情况下给出0的值
cout这是一个没有温度变量的解决方案:
#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;
}
#包括
使用名称空间std;
int main()
{
int n0=0;
int n1=1;
int n;
cout>n;
对于(int i=0;i/*作者:Eric Gitangu
日期:2015年7月29日
这个程序为32位数字范围输出斐波那契序列
假设:所有值都是+ve;无符号int在这里起作用
*/
#包括
#包括
#定义N功率(2.0,31.0)
使用名称空间std;
void fibionacci(无符号int&fib,无符号int&prevfib){
int temp=prevfib;
prevfib=fib;
fib+=温度;
}
void main(){
整数计数=0;
无符号整数fib=0u,prev=1u;
while(fib
#包括
使用名称空间std;
int fibonacciSeq(int k)
{
int num1=1;
int num2=1;
整数计数;
对于(int i=0;i,再看一看递归解决方案如何:
#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;
int fibonacciSeq(int k)
{
int num1=1;
int num2=1;
int count;
for(int i=0; i<k; i++)
{
if(i==1)
count=1;
else
{
count=num1+num2;
num1=num2;
num2=count;
}
}
return count;
}
void fibonacci(int n1,int n2,int numCount)
{
--numCount;
如果(numCount>0)
{
当然,CUT堆栈溢出是递归版本的一个限制。如果这不是问题,您也可以考虑递归Fibonacci的模板版本。
enterint fNum;
cout << "Enter a non negative number to print output fibonacci sequence: ";
cin >> fNum;
fibonacci(0, 1, fNum);
#包括
模板int Fib(){return Fib()+Fib();}
模板int Fib(){return 1;}
模板int Fib(){return 1;}
使用名称空间std;
int main()
{
//对于第10个斐波那契数。。。
cout简明版本:
#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;
}
intn,a{0},b{1};
标准:cin>>n;
而(n-->0){
std::cout您可以编写一个生成斐波那契级数的代码,避免使用if-else语句打印0和1,避免在循环外打印它们,避免使用“temp”整数。您可以通过使用-1和1初始化“first”和“second”变量来完成此操作,因此它们之间的和将为0,这是se的第一个器官瑞斯,和
#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;
}
#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;
}
#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);
}
/* 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 ";
}
}
#include <iostream>
using namespace std;
int fibonacciSeq(int k)
{
int num1=1;
int num2=1;
int count;
for(int i=0; i<k; i++)
{
if(i==1)
count=1;
else
{
count=num1+num2;
num1=num2;
num2=count;
}
}
return count;
}
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);
#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;
}
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);
}
#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;
}