挑战任务
动物园有一只小猴子喜欢吃桃子,不过它有个很独特的习惯,每次都把找到的桃子分成相等的两份,吃掉一份,留一份。如果不能等分,小猴子就会丢掉一个然后再分。第二天再继续这个过程,直到最后剩一个桃子了,也直接扔掉。
有一天,小猴子找到了n
个桃子,请问,它在吃这些桃子的过程中,一共要扔掉多少个桃子。
编程要求
- 补全方法
int getNum(int n)
,计算小猴子总共要扔掉多少个桃子。将最后的结果作为函数的返回值返回。
具体请参见后续测试样例。
测试说明
样例1
输入:
3
输出:
2
样例2
输入:
1544
输出:
3
#ifndef _TEST
#define _TEST
#include <iostream>
#include <vector>
using namespace std; class Task{
public:
int getNum(int n){
/********* Begin *********/
int count = 0;
while(n > 1)
{
if (n % 2== 0)
n = n/2;
else
{
n = n-1;
count = count+1;
}
}
return count+1;
/********* End *********/
}
};
#endif
#ifndef _TEST
#define _TEST
#include <iostream>
#include <vector>
using namespace std; class Task{
public:
vector<char> inversion(string str){
vector <char> vec;
int index = str.length();
while(index--){
vec.push_back(str[index]);
}
return vec;
}
};
#endif
#include <iostream>
#include <vector>
#include <stdio.h>
#include "Task.hpp" using namespace std; int main(){
string str;
getline(cin,str); Task tt;
vector <char> result;
result = tt.inversion(str);
for(int i = 0; i<str.length();i++){
cout << result[i];
}
cout << endl;
}
运行结果