万世不竭
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description
古人云:一尺之棰,日折其半,万世不竭。意思是说一尺长的短木棍,每天将其折成两半,那么一万年也折不完。说的非常有道理,但是如果我们设置一个最小单位的话,比如木棍小于 4cm 就视为不存在的,是可以折完的。但是我们需要折多少次,才能把一根木棍折完呢?
一根为长为奇数 l 的木棍折半为 l/2,l/2+1。
Input
输入数据有多组(数据组数不超过 100),到 EOF 结束。
对于每组数据,输入 1 行,包含 1 个整数 n (4 <= n <= 10^6),代表木棍长度。
Output
对于每组数据,输出一行,表示需要折多少次才能将木棍折完。
Example Input
12
Example Output
3
Hint
示例解释:
12 -> 6,6
6 -> 3,3
6 -> 3,3
#include <stdio.h>
int number;
int num(int n)
{
if(n < 4)
return number;
else
{
if(n % 2 == 0)
{
number++;
num(n / 2);
num(n / 2);
}
if(n % 2 != 0)
{
number++;
num(n / 2);
num(n / 2 + 1);
}
}
return number;
}
int main()
{
int n;
while(~scanf("%d", &n))
{
number = 0;
printf("%d\n", num(n));
}
return 0;
}