HDU-2018 母牛的故事 C/C++ 递推

3 篇文章 0 订阅
3 篇文章 0 订阅

HDU-2018 母牛的故事

Problem Description

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

Input

输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。 n=0表示输入数据的结束,不做处理。

Output

对于每个测试实例,输出在第n年的时候母牛的数量。 每个输出占一行。

Sample Input

2
4
5
0

Sample Output

2
4
6

My first passage on CSDN.The reason why I use English is that my tpewritting is so lame.
But it might not be a bad thing for me to do this in English.

I attempts for times but all I got are WA! At first I use recursion(di gui) to solve the peoblem,but I gave up.
Finally I searched it on the internet and knew all it need is just the recurision(di tui??)

(p.s Why digui and ditui use the same word in Eng??)

This is my code.

#include<stdio.h>
int main()
{
	int cow[56],n;
	cow[0]=0;cow[1]=1;cow[2]=2;cow[3]=3;//先确定前几年的个数,可以在纸上自己画一下 
	for(int i=4;i<=55;i++){//递推 
			cow[i]=cow[i-1]+cow[i-3];
		}
	while(scanf("%d",&n)==1&&n){//按题目要求输入输出 
		printf("%d\n",cow[n]);
	}
	return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值