HDU-2018 Story of Cow

See the ariticle on https://dyingdown.github.io/2019/12/20/HDU-2018%20Story-of-Cow/

Story of Cow

There is a cow that gives birth to a heifer at the beginning of each year. Each heifer starts in the fourth year and a heifer is born at the beginning of each year. Please program how many cows will there be in the year N?

Input

The input data consists of multiple test cases, each test case occupies one line, including an integer n (0 <n <55), the meaning of n is as described in the title.
n = 0 means the end of the input data, no processing.

Output

For each test case, the number of cows at year n is output.
One line per output.

Sample Input

2
4
5
0

Sample Output

2
4
6

Analysis

Let’s stimulate the situation by ourselves first.

Year12345678910
Number12346913192841

We can draw a conclusion that:
c o w [ i ] = c o w [ i − 3 ] + c o w [ i − 1 ]        ( i > 3 ) cow[i] = cow[i - 3] + cow[i - 1] \space \space\space \space\space \space (i>3) cow[i]=cow[i3]+cow[i1]      (i>3)

Code

#include<bits/stdc++.h>

using namespace std;

long long arr[1000];
int main() {
	arr[1] = 1; arr[2] = 2; arr[3] = 3;
	for(int i = 4; i <= 55; i ++) {
		arr[i] = arr[i - 1] + arr[i - 3];
	}
	int n;
	while(cin >> n, n) {
		cout << arr[n] << endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值