木头折断问题
题目:一个长度为n(double类型变量,n > 5)的木条,
可以在其2 / 5处折断,变为两段长度分别为2n / 5、3n / 5的木条;如果得到的木条的长度仍大于5,则继续按照上述方法折断,直到任意木条的长度均不大于5为止。编写递归函数,计算一个长度为n的木条,最后会被折断为多少根木条?
样例1:输入:4.8 输出:1
样例2:输入:6 输出:2
样例3:输入:20 输出:5
代码:
#include<stdio.h>
static int count=1;
int function(double n)
{
if (n < 5)
return count;
else
{
count++;
function(2 * n / 5);
function(3 * n / 5);
return count;
}
}
int main()
{
double n;
scanf("%lf", &n);
printf("%d", function(n));
return 0;
}
来源:CSDN
作者:qq_35926606
链接:https://blog.csdn.net/qq_35926606/article/details/103754515