自学内容网 自学内容网

嘤嘤不想打怪兽喵

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

嘤嘤会使用一种魔法:将血量为 xxx 的史莱姆分裂成两只血量为 ⌊x2⌋\lfloor \frac x 2 \rfloor⌊2x​⌋ (即除以 2 向下取整)的史莱姆,当史莱姆血量为 0 时,史莱姆会死亡。

嘤嘤想知道,消灭一只血量为 hhh 的史莱姆最少需要使用几次魔法喵~。

输入描述:

给定一个整数 h(1≤h≤109)h(1 \leq h \leq 10^9)h(1≤h≤109) ,表示史莱姆的血量。

输出描述:

 

输出一个整数表示答案。

示例1

输入

复制5

5

输出

复制7

7

说明

 

第1次使用魔法后,史莱姆变成:{2,2};

第2次使用魔法后,史莱姆变成:{1,1,2};

第3次使用魔法后,史莱姆变成:{0,0,1,2},有2只史莱姆死亡;

第4次使用魔法后,史莱姆变成:{0,0,2},有2只史莱姆死亡;

第5次使用魔法后,史莱姆变成:{1,1};

第6次使用魔法后,史莱姆变成:{0,0,1},有2只史莱姆死亡;

第7次使用魔法后,史莱姆变成:{0,0},有2只史莱姆死亡。

在使用7次魔法后,所有史莱姆都被消灭了。

递归秒了。应该能有规律的。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<vector>
#include<math.h>
#include<iomanip>
#include<set>
#include<queue>
#include<stack> 
#include<map>
#include<list>
#include <stdlib.h>
#include<deque>
#include <stdlib.h>
#include <time.h>
#include<cstdlib>
using namespace std;
long long a, b;
int fun(int n)
{
if (n == 1)
{
return 1;
}
else
{
return fun(n / 2) + fun(n / 2) + 1;
}
}
int main()
{
cin >> a ;
cout << fun(a);
}


原文地址:https://blog.csdn.net/Q210617/article/details/140175318

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!