当前位置:网站首页>Topic 1004: the story of cows (recursion)
Topic 1004: the story of cows (recursion)
2022-07-01 12:35:00 【51CTO】
List of articles
- Question
- Ideas
- Code
Question
Title Description
There is a cow , It gives birth to a heifer every year . Each heifer starts from the fourth year , A heifer is born at the beginning of each year . Please programmatically implement in Section n In the year , How many cows are there ?
Input
The input data consists of multiple test cases , One line per test instance , Include an integer n(
0
<
n
<
55)
,n The meaning of is described in the title .
n
=
0
Indicates the end of the input data , Don't deal with it .
Output
For each test case , The output is in the second n The number of cows in the year .
One line per output .
The sample input
2
4
5
0
Sample output
2
4
6
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
Ideas
The first n Number of cattle in = The first n-1 Number of cattle in + The first n-3 Number of years ago ( Calves can be born in four years )
Code
# Recurrence
list
= []
# Definition list
list.
append(
0)
# Go to list Add initial data
list.
append(
1)
# The number of cows in the first year is 1
list.
append(
2)
# The number of cows in the first year is 2
list.
append(
3)
# The number of cows in the first year is 3
for
i
in
range(
4,
1000):
# Before pretreatment 1000 Number of cows in
list.
append(
list[
i
-
1]
+
list[
i
-
3])
while
True:
n
=
int(
input())
# Convert to integer
if
n
==
0:
#n ==0 Exit from time
break
print(
list[
n])
# It has been pretreated , All direct output is OK
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
边栏推荐
- [shell programming] - shell introductory learning
- GPS 数据中的精度因子(DOP)与协方差之间的关系 (参考链接)
- Teach you to complete the actual battle of image classification hand in hand -- Image Recognition Based on convolutional neural network
- 微信小程序 – 80个实用的微信小程序项目实例
- Huawei interview question: Recruitment
- Double linked list related operations
- Good luck brought by years of persistence
- Stack-------
- MySQL workbench data modeling function
- 买卖其实也有风险
猜你喜欢

基因检测,如何帮助患者对抗疾病?
![[some notes]](/img/91/7657f90b50f012736579b1585b4ade.jpg)
[some notes]

redis探索之缓存击穿、缓存雪崩、缓存穿透
![[datawhale202206] pytorch recommendation system: recall model DSSM & youtubednn](/img/f2/7931952b832e84d7b8f2615906f33f.png)
[datawhale202206] pytorch recommendation system: recall model DSSM & youtubednn
![[Suanli network] technological innovation of Suanli Network -- key technology of operation service](/img/80/6e3648c88d309516d4bc29db9c153c.jpg)
[Suanli network] technological innovation of Suanli Network -- key technology of operation service

Indefinite integral

redis探索之缓存一致性

The operation process of using sugar to make a large data visualization screen

双链表有关操作

手机便签应用
随机推荐
QT 播放器之列表[通俗易懂]
被锡膏坑了一把
MySQL data table creation
Using burpsuite to capture app packages
How to install php7 and perform performance test using yum
【20211129】Jupyter Notebook遠程服務器配置
《MATLAB 神经网络43个案例分析》:第40章 动态神经网络时间序列预测研究——基于MATLAB的NARX实现
Efforts at the turn of the decade
【脑洞大开】《西潮》及《走向世界丛书》
CPI教程-异步接口创建及使用
[datawhale202206] pytorch recommendation system: multi task learning esmm & MMOE
Understanding of NAND flash deblocking
Chained storage of queues
题目 1004: 母牛的故事(递推)
【20220605】文献翻译——虚拟现实中的可视化:一个系统的回顾
Blocking sockets的读写操作该怎么玩?
腾讯安全发布《BOT管理白皮书》|解读BOT攻击,探索防护之道
Sleep quality today 79 points
redis探索之缓存一致性
使用nvm管理nodejs(把高版本降级为低版本)