当前位置:网站首页>There is a cow, which gives birth to a heifer at the beginning of each year. Each heifer has a heifer at the beginning of each year since the fourth year. Please program how many cows are there in the
There is a cow, which gives birth to a heifer at the beginning of each year. Each heifer has a heifer at the beginning of each year since the fourth year. Please program how many cows are there in the
2022-07-07 15:13:00 【Lele ~ll】
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 .
Sample input copy
2 4 5 0
Sample output copy
2 4 6
The code is as follows :
Two ways of thinking , Recursion and dynamic programming
1, recursive
Recursion first considers the end condition of recursion --》 Serve as n==1 When Only one cow ,,return 1
In other cases : The first n year The number of cattle is :n-1 Number of years and The number of new cows born this year
, The number of new cows born this year -- Is the number of fertile cows this year -- Namely n-3 year Number of cows per hour
So we have to discuss n--- When n<=3 When There is only one fertile cow ,
Pseudo code :
if (n == 1)
{
return 1;
}
if (n <= 3)
{
return number(n - 1) + 1;
}
else
{
return number(n - 1) + number(n - 3);
}
This code is OK, but It's easy to time out
Let's continue to consider
First year 1 head
In the second year 2 head
The third year 3 head
No new cows have been born in the past three years , And the number of cows is equal to the number of years
improvement :
if (n <= 3)
{
return n;
}
else
{
return number(n - 1) + number(n - 3);
}
The code is as follows :
#include<stdio.h>
int number(int n)
{
/*if (n == 1)
{
return 1;
}*/
if (n <= 3)
{
//return number(n - 1) + 1;
return n;
}
else
{
return number(n - 1) + number(n - 3);
}
}
int main()
{
int arr[1000]={0};
int n = 0;
int i = 0;
scanf("%d", &n);
while (n != 0)
{
arr[i] = n;
i++;
scanf("%d", &n);
}
for (i = 0; arr[i] != '\0'; i++) {
printf("%d\n", number(arr[i]));
}
return 0;
}
The second method : Dynamic programming --- It's about finding the rules
equally -- Don't go to this more abstract problem look for N The number of cattle corresponding to the year ,,, This will be more difficult ,, We introduce an array ,, Number of cattle stored every year , Horizontal law finding ,, The essential idea is the same as recursion ,
The code is as follows :
#include<stdio.h>
int main()
{
int arr[1000]={0};
int n = 0;
int i = 1;
scanf("%d", &n);
int max = n;
while (n != 0)
{
arr[i] = n;
i++;
if (n >= max)
{
max = n;
}
scanf("%d", &n);
}
int number[1000] = { 0 };// The number of cows in the corresponding year
for (i = 1; i <= max; i++)
{
if (i <= 3)
{
number[i] = i;
}
else
{
number[i] = number[i - 1] + number[i - 3];
}
}
for (i = 1; arr[i] != '\0'; i++)
{
printf("%d\n", number[arr[i]]);
}
return 0;
}Take a look at the speed of two codes ( The top one is for Dynamic programming solution

obviously ----- Dynamic programming ---- still Be quick
Try dynamic programming Fibonacci sequence ~!@#¥%……&*(
边栏推荐
- Xiaomi's path of chip self-development
- 广州开发区让地理标志产品助力乡村振兴
- [follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code
- Ctfshow, information collection: web2
- #yyds干货盘点# 解决名企真题:交叉线
- Concurrency Control & NoSQL and new database
- 上半年晋升 P8 成功,还买了别墅!
- What are PV and UV? pv、uv
- 【OBS】RTMPSockBuf_Fill, remote host closed connection.
- Read PG in data warehouse in one article_ stat
猜你喜欢

Protection strategy of server area based on Firewall
![[data mining] visual pattern mining: hog feature + cosine similarity /k-means clustering](/img/a4/7320f5d266308f6003cc27964e49f3.png)
[data mining] visual pattern mining: hog feature + cosine similarity /k-means clustering

Five pain points for big companies to open source

【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類

IDA pro逆向工具寻找socket server的IP和port

有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?

Ctfshow, information collection: Web3

Mathematical modeling -- what is mathematical modeling
![leetcode:648. Word replacement [dictionary tree board + find the shortest matching prefix among several prefixes]](/img/3e/cdde4b436821af8700eb65d35e8f59.png)
leetcode:648. Word replacement [dictionary tree board + find the shortest matching prefix among several prefixes]

Ctfshow, information collection: web6
随机推荐
CTFshow,信息搜集:web1
Jetson AGX Orin CANFD 使用
Ctfshow, information collection: web4
[server data recovery] a case of RAID data recovery of a brand StorageWorks server
What is data leakage
Cocoscreator resource encryption and decryption
What are PV and UV? pv、uv
CTFshow,信息搜集:web13
@Introduction and three usages of controlleradvice
PG基础篇--逻辑结构管理(锁机制--表锁)
一个需求温习到的所有知识,h5的表单被键盘遮挡,事件代理,事件委托
激光雷达lidar知识点滴
JSON parsing instance (QT including source code)
CTFshow,信息搜集:web9
Ctfshow, information collection: web14
Briefly describe the working principle of kept
Several ways of JS jump link
Novel Slot Detection: A Benchmark for Discovering Unknown Slot Types in the Dialogue System
数学建模——什么是数学建模
【OBS】RTMPSockBuf_ Fill, remote host closed connection.