当前位置:网站首页>蓝桥杯 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;
}
边栏推荐
猜你喜欢
随机推荐
9_ Logistic regression
20220720 toss deeplobcut2
Training team lpoj round10 d come minion!
放图仓库-3(功能图像)
Sliding window problem summary
13_集成学习和随机森林(Ensemble Learning and Random Forests)
Abstract classes and interfaces (sorting out some knowledge points)
Codeforces E. maximum subsequence value (greed + pigeon nest principle)
C语言 关机小程序
c语言 static运用,灵活改变生命周期,让你写代码如鱼得水
Midge paper reading notes
4-4 object lifecycle
Torch. correlation function
In JS, the common writing methods and calling methods of functions - conventional writing, anonymous function writing, taking the method as an object, and adding methods to the object in the construct
01 knapsack problem 416. Segmentation and equal sum subset -494. Goal and
Codeforces C1. Simple Polygon Embedding
C and pointer Chapter 18 runtime environment 18.7 problems
[PCB open source sharing] stc8a8k64d4 development board
Tree and binary tree (learning notes)
Codeforces D. Buying Shovels









