当前位置:网站首页>华为面试题: 分糖果
华为面试题: 分糖果
2022-06-30 02:55:00 【四库全书的酷】
题目
小明从糖果盒中随意抓一把糖果
每次小明会取出一半的糖果分给同学们
当糖果不能平均分配时
小明可以从糖果盒中(假设盒中糖果足够)取出一个或放回一个糖果
小明至少需要多少次(取出放回和平均分配均记一次)能将手中糖果分至只剩一颗
输入描述:
抓取糖果数(小于1000000):15
输出描述:
最少分至一颗糖果的次数:5
示例1:
输入
15
输出
5
备注
解释:(1) 15+1=16;
(2) 16/2=8;
(3) 8/2=4;
(4) 4/2=2;
(5) 2/2=1;
代码
注意点:是统计取回和放进去的次数!!
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
int count = 0;
for(int i = n; i != 1;i /= 2,count++){
if(i == 3){
cout << count+2 << endl;
return 0;
}
//如果能被2整除,就不容取出或者放回;
//如果不能被2整除,统一一下+1和-1后谁的最少!;这里使用了局部最优!!
if(i % 2 != 0){
if((i + 1)/2 % 2 == 0) i+=1;
else --i;
++count;
}
}
cout << count << endl;
return 0;
}
边栏推荐
- threejs 镜子案例Reflector 创建镜子+房子搭建+小球移动
- 发现mariadb数据库时间晚了12个小时
- Global and Chinese markets for light cargo conveyors 2022-2028: Research Report on technology, participants, trends, market size and share
- Hands on in-depth learning notes (XV) 4.1 Multilayer perceptron
- Lua 基础知识
- Linear algebra Chapter 3 summary of vector and vector space knowledge points (Jeff's self perception)
- Global and Chinese market for defense network security 2022-2028: Research Report on technology, participants, trends, market size and share
- 中断操作:AbortController学习笔记
- What is digicert smart seal?
- FDA mail security solution
猜你喜欢

Cross domain, CORS, jsonp

Uniapp address translation latitude and longitude

How to switch ipykernel to a different CONDA virtual environment in jupyterlab?

What should academic presentation /ppt do?

Precautions for purchasing wildcard SSL certificate

2. < tag dynamic programming and 0-1 knapsack problem > lt.416 Split equal sum subset + lt.1049 Weight of the last stone II
![[dry goods sharing] the latest WHQL logo certification application process](/img/c3/37277572c70b0af944e594f0965a6c.png)
[dry goods sharing] the latest WHQL logo certification application process
![[Postgres] Postgres database migration](/img/45/7074aa766640160a3b6f00b109cb2f.png)
[Postgres] Postgres database migration

Software testing skills, JMeter stress testing tutorial, transaction controller of logic controller (25)

JMeter obtains cookies across thread groups or JMeter thread groups share cookies
随机推荐
2022 underground coal mine electrical test and underground coal mine electrical simulation test
CMake教程系列-02-使用cmake代码生成二进制
How to use vant to realize data paging and drop-down loading
原生JS怎么生成九宫格
重磅来袭--UE5的开源数字孪生解决方案
自定义JvxeTable的按钮及备注下$set的用法
Global and Chinese market for defense network security 2022-2028: Research Report on technology, participants, trends, market size and share
微信小程序页面跳转以及参数传递
Unity3D UGUI强制刷新Layout(布局)组件
JMeter obtains cookies across thread groups or JMeter thread groups share cookies
How to set password complexity and timeout exit function in Oracle
Linear algebra Chapter 4 Summary of knowledge points of linear equations (Jeff's self perception)
Global and Chinese market of ERP software for garment and textile industries 2022-2028: Research Report on technology, participants, trends, market size and share
Cmake tutorial series-01-minimum configuration example
Unity TimeLine 数据绑定
The rigorous judgment of ID number is accurate to the last place in the team
shell统计某个字符串最后一次出现的位置之前的所有字符串
HTA introductory basic tutorial | GUI interface of vbs script HTA concise tutorial, with complete course and interface beautification
备忘一下es6的export/import和类继承的用法
unity的text首列有标点符号咋办