欢迎来到IT嘟嘟的博客

人生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。
扩大
缩小

全国高校绿色计算大赛 预赛第一阶段(C++)第2关:扔桃子

挑战任务

动物园有一只小猴子喜欢吃桃子,不过它有个很独特的习惯,每次都把找到的桃子分成相等的两份,吃掉一份,留一份。如果不能等分,小猴子就会丢掉一个然后再分。第二天再继续这个过程,直到最后剩一个桃子了,也直接扔掉。

有一天,小猴子找到了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;
}

  

运行结果

 

posted on 2018-10-29 09:38  IT嘟嘟  阅读(375)  评论(0编辑  收藏  举报

导航