当前位置:网站首页>华为面试题: 分糖果
华为面试题: 分糖果
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;
}
边栏推荐
- Five cheapest wildcard SSL certificate brands
- How to use vant to realize data paging and drop-down loading
- Welfare lottery | what are the highlights of open source enterprise monitoring zabbix6.0
- GTK interface programming (II): key components
- High paid programmers & interview questions series 63: talk about the differences between sleep (), yield (), join (), and wait ()
- Cmake tutorial series-03-dependency management
- Simulate activity startup mode in compose
- Global and Chinese market for defense network security 2022-2028: Research Report on technology, participants, trends, market size and share
- CMake教程系列-02-使用cmake代碼生成二進制
- c#控制台格式化代码
猜你喜欢

Use compose to realize the effect of selecting movie seats by panning tickets

JMeter obtains cookies across thread groups or JMeter thread groups share cookies

Customize the buttons of jvxetable and the usage of $set under notes

LeetCode 3. Longest substring without duplicate characters

Summary of PHP test sites encountered in CTF questions (I)

IDEA 远程调试 Remote JVM Debug

CMake教程系列-02-使用cmake代碼生成二進制

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

Raki's notes on reading paper: named entity recognition as dependency parsing

How to prevent phishing emails? S/mime mail certificate
随机推荐
Global and Chinese markets of liquid optical waveguides 2022-2028: Research Report on technology, participants, trends, market size and share
How can redis+aop customize annotations to achieve flow restriction
三层交换机和二层交换机区别是什么
Software testing skills, JMeter stress testing tutorial, transaction controller of logic controller (25)
Wechat applet page Jump and parameter transfer
Template parameter package and function parameter package
How to modify and add fields when MySQL table data is large
Multi card server usage
(图论) 连通分量(模板) + 强连通分量(模板)
Detailed explanation of minimum stack
What is digicert smart seal?
Raki's notes on reading paper: discontinuous named entity recognition as maximum clique discovery
2. successfully solved bug:exception when publishing [Failed to connect and initialize SSH connection...
Distributed file system fastdfs
Interrupt operation: abortcontroller learning notes
Cmake tutorial series-01-minimum configuration example
LeetCode 3. Longest substring without duplicate characters
What is the difference between a layer 3 switch and a layer 2 switch
Some configuration details about servlet initial development
FDA mail security solution