当前位置:网站首页>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.
边栏推荐
- 腾讯总考epoll, 很烦
- Share several tools for designing exquisite circuit diagrams
- System test UI test summary and questions (interview)
- Chapter 14 signals (IV) - examples of multi process tasks
- Onenet Internet of things platform - create mqtts products and devices
- [20220605] Literature Translation -- visualization in virtual reality: a systematic review
- 【邂逅Django】——(二)数据库配置
- CPI教程-异步接口创建及使用
- Exploration and practice of inress in kubernetes
- 关于NAND FLASH解扣的认识
猜你喜欢

Onenet Internet of things platform - mqtt product equipment upload data points
![[20211129] configuration du serveur distant du carnet de notes jupyter](/img/7c/79c9fcb91bde75e954dc3ecf9f5afd.png)
[20211129] configuration du serveur distant du carnet de notes jupyter
![[datawhale202206] pytorch recommendation system: precision model deepfm & DIN](/img/4f/8799016731a2c1647b6f2f4d96b754.png)
[datawhale202206] pytorch recommendation system: precision model deepfm & DIN
![[20220605] Literature Translation -- visualization in virtual reality: a systematic review](/img/11/6c42957186bf530e8f9d4025a40197.png)
[20220605] Literature Translation -- visualization in virtual reality: a systematic review

One year anniversary of bitbear live studio, hero rally order! I invite you to take a group photo!

Sleep quality today 79 points

Circular linked list--

redis探索之缓存击穿、缓存雪崩、缓存穿透
![[datawhale202206] pytorch recommendation system: multi task learning esmm & MMOE](/img/8f/64fea641730795a2b5252cc2c8cdd2.png)
[datawhale202206] pytorch recommendation system: multi task learning esmm & MMOE

Ansible相关内容梳理
随机推荐
ASP. Net core 6 from entry to enterprise level practical development application technology summary
网络socket的状态要怎么统计?
ASTM D 3801 vertical burning test of solid plastics
Mysql database knowledge collation
[20211129] jupyter notebook remote server configuration
BIM and safety in road maintenance-buildSmart Spain
Digital signal processing -- Design of linear phase (Ⅱ, Ⅳ) FIR filter (2)
fatal error: execution: 没有那个文件或目录
Tencent Li Wei: deeply cultivate "regulatory technology" to escort the steady and long-term development of the digital economy
redis探索之缓存一致性
[datawhale202206] pytorch recommendation system: recall model DSSM & youtubednn
Eurake分区理解
基因检测,如何帮助患者对抗疾病?
2022-06-28-06-29
数论基础及其代码实现
Wechat applet - 80 practical examples of wechat applet projects
Zero copy technology of MySQL
[datawhale202206] pytorch recommendation system: precision model deepfm & DIN
Pandas reads MySQL data
【20211129】Jupyter Notebook远程服务器配置