题意:http://acm.hdu.edu.cn/showproblem.php?pid=2018
解答:
#include<stdio.h>
int cow(int a)
{
if(a<5)return a;
else return cow(a-1)+cow(a-3);
}
int main()
{
int a;
while(scanf("%d",&a),a)
printf("%d\n",cow(a));
return 0;
}
本文介绍了一个经典的递归算法题目——HDU 2018 牛问题,并提供了一种使用 C 语言实现的递归解决方案。通过分析问题背景和逐步解析代码逻辑,帮助读者理解如何利用递归思想解决实际问题。
题意:http://acm.hdu.edu.cn/showproblem.php?pid=2018
解答:
#include<stdio.h>
int cow(int a)
{
if(a<5)return a;
else return cow(a-1)+cow(a-3);
}
int main()
{
int a;
while(scanf("%d",&a),a)
printf("%d\n",cow(a));
return 0;
}
782

被折叠的 条评论
为什么被折叠?