当前位置:网站首页>One book 1201 Fibonacci sequence
One book 1201 Fibonacci sequence
2022-07-27 08:14:00 【Bamboo monk-】
Original link
http://ybt.ssoier.cn:8088/problem_show.php?pid=1201
【 Title Description 】
Fibonacci sequence refers to such a sequence : The first and second numbers of the sequence are 1, Next, each number is equal to the front 2 Sum of numbers .
Give a positive integer a, Request the... In Fibonacci sequence a What's the number .
【 Input 】
The first 1 Row is the number of groups of test data n, Follow behind n Line input . Each group of test data accounts for 1 That's ok , Include a positive integer a(1<=a<=20).
【 Output 】
The output is n That's ok , Each line of output corresponds to an input . The output should be a positive integer , It is the... In Fibonacci sequence a The size of the number .
【 sample input 】
4 5 2 19 1
【 sample output 】
5 1 4181 1
Examine the subject
Fibonacci number one a Item equal to a-1 Add item a-2 term . Just recursively find the first two items .
step
1. Define recursive call function
int f(int x)
{
if(x==1 || x==2) return 1;// The values of the first and second terms are 1
else return f(x-1)+f(x-2); // Recursively call
}
2. The main function
int main()
{
cin>>n;// share n Group
while(n--)// Repeat input
{
cin>>a;
cout<<f(a)<<endl;// Please a Item value
}
return 0;
}Complete code
#include<iostream>
using namespace std;
int a,n;
int f(int x)
{
if(x==1 || x==2) return 1;// The values of the first and second terms are 1
else return f(x-1)+f(x-2); // Recursively call
}
int main()
{
cin>>n;// share n Group
while(n--)// Repeat input
{
cin>>a;
cout<<f(a)<<endl;// Please a Item value
}
return 0;
}Novice , Your advice are most welcome
边栏推荐
- Idea remote debugging
- Use of "PHP Basics" delimiters
- Shenzhi Kalan Temple
- [ten thousand words long article] thoroughly understand load balancing, and have a technical interview with Alibaba Daniel
- 2020 International Machine Translation Competition: Volcano translation won five championships
- Lua iterator
- 数据提取1
- "PHP Basics" tags in PHP
- 代码接口自动化的有点
- Stored procedures and functions
猜你喜欢

How to play with the purchase of SAP variant materials? Look at this article and you will understand

Translation character '/b' in C #

阿里云国际版回执消息简介与配置流程

File name wildcard rules for kettle

Demo:st05 find text ID information

SSTI template injection

I can't figure out why MySQL uses b+ trees for indexing?
![[NPUCTF2020]ReadlezPHP 1](/img/d9/590446b45f917be3f077a9ea739c20.png)
[NPUCTF2020]ReadlezPHP 1

Stored procedures and functions
![[ten thousand words long article] thoroughly understand load balancing, and have a technical interview with Alibaba Daniel](/img/fc/1ee8b77d675e34da2eb8574592c489.png)
[ten thousand words long article] thoroughly understand load balancing, and have a technical interview with Alibaba Daniel
随机推荐
Promise details
二零二零年终总结
Things come to conform, the future is not welcome, at that time is not miscellaneous, neither love
Kalibr calibration realsensed435i -- multi camera calibration
Leetcode54. Spiral matrix
Database startup report error_ user_ connect_ times &gt; 0 error
[NPUCTF2020]ReadlezPHP 1
一文速览EMNLP 2020中的Transformer量化论文
Data extraction 2
C commissioned use cases
想让照片中的云飘起来?视频编辑服务一键动效3步就能实现
[netding cup 2020 Qinglong group]areuserialz (buuctf)
How does slf4j configure logback?
Demo:st05 find text ID information
What is a rebound shell? What's the use of bouncing shells?
Shell scripts related
瑞芯微RK3399-I2C4挂载EEPROM的修改案例
Containerd failed to pull private database image (kubelet)
Translation character '/b' in C #
How to play with the purchase of SAP variant materials? Look at this article and you will understand