zoukankan      html  css  js  c++  java
  • 全国高校绿色计算大赛 预赛第一阶段(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;
    }
    

      

    运行结果

  • 相关阅读:
    ubuntu下搭建lnmp
    mysql常用命令
    nginx 配置 location 语法 正则表达式
    网站策划方案写作、演示标准[转]
    WEB3.0标准的核心
    也谈论坛“BBS2.0”的十大升级方向 [网摘]
    未来网站策划呈现5大趋势 [网摘]
    关于Web 2.0 网站的创业思考[转]
    关于web3.0的标准:吃喝买卖随己
    左右均自适应高度
  • 原文地址:https://www.cnblogs.com/277223178dudu/p/9868838.html
Copyright © 2011-2022 走看看