当前位置:网站首页>华为面试题: 分糖果
华为面试题: 分糖果
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;
}
边栏推荐
- GTK interface programming (II): key components
- 备忘一下es6的export/import和类继承的用法
- Welfare lottery | what are the highlights of open source enterprise monitoring zabbix6.0
- CMake教程系列-01-最小配置示例
- Add a custom button to jvxetable
- Mysql表数据比较大情况下怎么修改添加字段
- Which is a good foreign exchange trading platform? Is it safe to have regulated funds?
- How do I enable assembly binding logging- How can I enable Assembly binding logging?
- Call collections Sort() method, compare two person objects (by age ratio first, and by name ratio for the same age), and pass lambda expression as a parameter.
- (图论) 连通分量(模板) + 强连通分量(模板)
猜你喜欢

Visual HTA form designer htamaker interface introduction and usage, Download | HTA VBS visual script writing

What should academic presentation /ppt do?

Wechat applet page Jump and parameter transfer

Enlightenment from the revocation of Russian digital certificate by mainstream CA: upgrade the SSL certificate of state secret algorithm to help China's network security to be autonomous and controlla

Linear algebra Chapter 3 summary of vector and vector space knowledge points (Jeff's self perception)

What is the difference between a layer 3 switch and a layer 2 switch

Call collections Sort() method, compare two person objects (by age ratio first, and by name ratio for the same age), and pass lambda expression as a parameter.

2.8 【 weight of complete binary tree 】

可视化HTA窗体设计器-HtaMaker 界面介绍及使用方法,下载 | HTA VBS可视化脚本编写

如何在 JupyterLab 中把 ipykernel 切换到不同的 conda 虚拟环境?
随机推荐
Formal and actual parameters, value passing and address passing
[untitled]
Customize the buttons of jvxetable and the usage of $set under notes
(图论) 连通分量(模板) + 强连通分量(模板)
[oiclass] chess piece
发现mariadb数据库时间晚了12个小时
Global and Chinese market for defense network security 2022-2028: Research Report on technology, participants, trends, market size and share
Azure 开发者新闻快讯丨开发者6月大事记一览
CMake教程系列-02-使用cmake代碼生成二進制
Cmake tutorial series -05- options and variables
Idea remote debugging remote JVM debug
What should academic presentation /ppt do?
002 color classification
Global and Chinese market of subscription revenue management software 2022-2028: Research Report on technology, participants, trends, market size and share
GTK interface programming (II): key components
Mysql提取表字段中的字符串
(graph theory) connected component (template) + strongly connected component (template)
如何在 JupyterLab 中把 ipykernel 切换到不同的 conda 虚拟环境?
What about punctuation in the first column of unity text
Heavy attack -- ue5's open source digital twin solution