当前位置: 首页 > news >正文

斐波那契数列 (Fibonacci Sequence) C++

Description

使用递归实现一个函数,打印斐波那契数列的第n项。
斐波那契数列的定义如下:

  • F(0) = 0, F(1) = 1, F(n) = F(n-1) + F(n-2)
    递归思路:通过递归调用前两个数的和来计算当前的 Fibonacci 数字,直到达到递归的基本情况F(0)F(1)

Use recursion to implement a function to print then-th Fibonacci number.
The Fibonacci sequence is defined as:

  • F(0) = 0, F(1) = 1, F(n) = F(n-1) + F(n-2)
    Recursive idea: Calculate the current Fibonacci number by recursively summing the previous two numbers, until reaching the base casesF(0)orF(1).

Input

输入一个整数n(0 ≤ n ≤ 20),代表要求的 Fibonacci 数列项。

Input an integern(0 ≤ n ≤ 20), representing the position of the Fibonacci number to be computed.

Output

输出斐波那契数列的第n项。

Output then-th Fibonacci number.

Sample Input 1 

6

Sample Output 1

8

#include<iostream>
using namespace std;
int F(int x)
{
	if(x==0)
		return 0;
	else if (x==1)
		return 1;
	else
		return F(x-1)+F(x-2);
		
		
	
	
}


int main()
{	
	
	int endnumber;
	cin >> endnumber;
	cout<< F(endnumber);
	return 0;
}

相关文章:

  • 0.DockerCE起步之Linux相关【完善中】
  • 提示词 (Prompt)
  • 树上搜索 第32次CCF-CSP计算机软件能力认证
  • 激光院董事长龚赤坤到北京研发中心检查指导工作
  • 深入解析 Spring AI ChatClient:构建高效 AI 应用的终极指南
  • 2025年3月 Scratch图形化四级 真题解析 中国电子学会全国青少年软件编程等级考试
  • ida 使用记录
  • 基于javaweb的SpringBoot新闻视频发布推荐评论系统(源码+部署文档)
  • Windows系统Python多版本运行解决TensorFlow安装问题(附详细图文)
  • 【我的创作纪念日】
  • 使用MVC模式开发cocos游戏功能
  • 基于springboot餐饮连锁店管理系统
  • HTML — 浮动
  • 2-刷力扣问题记录
  • .py文件和.ipynb文件的区别:完整教程
  • 【安装配置教程】在linux使用nginx部署vue项目
  • 【玩泰山派】5、点灯,驱动led (使用python库操作)
  • GMSL 使用 GPIO Forward 功能实现 Frame Sync
  • MetaGPT深度解析:重塑AI协作开发的智能体框架实践指南
  • 云服务器租用费用都受哪些因素影响?
  • 济南行知网网站建设/全国最新实时大数据
  • 南京网站制作公司电话/网络项目平台
  • 西安做网站优化的公司/企业seo排名外包
  • 湖北省精神文明建设委员会网站/兰州压热搜
  • 禅城网站制作/域名查询注册商
  • 做网站需要ftp/文明seo技术教程网