当前位置:网站首页>蓝桥杯 1004 [递归]母牛的故事
蓝桥杯 1004 [递归]母牛的故事
2022-07-26 22:38:00 【沉梦昂志浮生若梦】
- 有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
- 输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。n=0表示输入数据的结束,不做处理。
- 样例输入2 4 5 0
- 样例输出2 4 6
思路
对于规律题首先要写出更多的输入和输出来找寻规律
1 2 3 4 5 6 7 8 9
1 2 3 4 6 9 13 19 28
观察发现为一个递归
F(n) = F(n-1) +F(n-3) 当且仅当 n>=4
#include <stdio.h>
int fun(int n)
{
if (n > 0 && n < 4)
{
return n;
}
return fun(n - 1) + fun(n - 3);
}
int main()
{
int n;
while (scanf("%d", &n) && n>0)
{
printf("%d \n", fun(n));
}
return 0;
}
边栏推荐
- PTA 7-1 play with binary tree
- Deploy yolov5 error reporting in pycharm
- V-viewer use
- Draw impact function
- LeetCode题目——数组篇
- Alexnet (pytoch Implementation)
- Find method of web page parsing by crawler
- Go exceed API source code reading (IV) -- save (), SaveAs (name string)
- Codeforces d.constructing the array (priority queue)
- 20220720折腾deeplabcut2
猜你喜欢

8_多项式回归及模型泛化(Polynomial Regression and Model Generalization)

查看 Anaconda 创建环境的位置

2022-07-17:1, 2, 3... N-1, N, n+1, n+2... In this sequence, only one number has repetition (n). This sequence is unordered. Find the repeated number n. This sequence is ordered. Find the repeated numb

知识蒸馏——pytorch实现

Request attribute in crawler

信号与系统学习零输入响应

10_ Evaluate classification

Configure deeplobcut 1 with your head covered

MySQL optimization

Complete review of parsing web pages
随机推荐
Oracle data guard service, process and protection mode
Viterbi Viterbi decoding bit error rate simulation, modulation is QPSK, channel is Gaussian white noise
Mysql database complex operations: Database Constraints, query / connect table operations
动态联编和静态联编、以及多态
01 knapsack problem 416. Segmentation and equal sum subset -494. Goal and
Class and object notes I
c语言 比大小的多种描述,不要只拘泥于一种写法
Recbole use 1
Search engine hijacking of black hat SEO
Find method of web page parsing by crawler
蒙着头配置deeplabcut 1
Fourier analysis (basic introduction)
Relationship between limit, continuity, partial derivative and total differential of multivariate function (learning notes)
Pyautogui usage example
Torch. correlation function
Arcgis和Cass实现断面展高程点
Anaconda => PyCharm => CUDA => cudnn => PyTorch 环境配置
Deployment of yolov5 on Jetson nano deepstream
Sliding window problem summary
信号与系统学习零输入响应