#Description
ccc is a VERY VERY BIG fans of Naruto(漩涡鸣人). Naruto’s perseverance and passion attract him a lot, so does Naruto’s skill “Kagebunsin no jyutu”(影分身术). Actually, ccc knows more details about “Kagebunsin no jyutu” than most of us. When Naruto makes an new illusion(幻象), his Chakra will divide into two parts A and B, and increase Naruto’s weariness value by absolutely value of (A - B).
For example, at first Naruto have 5 Chakra, then he makes an illusion who has 2 Chakra. So he left 3 Chakra, and increase his weariness value by 1 (3 - 2). If he make a new illusion again and give him 2 Chakra, then he only left 1 Chakra and his weariness value changes to 2. Naruto cannot make a new illusion unless his Chakra is bigger than 1. What’s more, Naruto’s illusion can also use “Kagebunsin no jyutu” and the weariness they gains will also return to Naruto himself.
Now Naruto wants to make as many illusions as he can because Kakashi Sensei wants to teach him a new Ninjyutsu(忍术) and minimum his weariness value.
#Input
The first line of input contains an integer T, indicating the number of test cases, then T lines follow , each line contains a positive integer N, indicating the number of Naruto’s Chakra.
0 < N < 10000000;
#Output
For each case ,output the minimum weariness value of Naruto when he makes the most illusions.
#Sample Input
2
5
9
#Sample Output
2
3
解题思路:
明白这道题的意思,其实就是n为偶数时,分为两个相等的数(分得的数可能为奇数或偶数),且weariness value of Naruto为0;n为奇数时则分为一大一小两个数,weariness value of Naruto为1;
然后分得的数一直往下分,直到分得的数为1时停止。
解题我采用了递归函数去算weariness value of Naruto的值。
代码:
#include<cstdio>
int count=0;
int solution(int n){
if(n<2) return 0;
if(n%2==0) {
return solution(n/2)*2;//*两个数相同结果乘2*
}
else
{
return solution(n/2)+solution(n/2+1)+1;//*一大一小差值为1所以是+1并持续分*
}
return count;
}
int main(){
int t;
scanf("%d",&t);
for(int i=0;i<t;i++){
int n;
scanf("%d",&n);
printf("%d\n",solution(n));
count=0;
}
return 0;
}