当前位置:网站首页>Huawei interview question: divide candy
Huawei interview question: divide candy
2022-06-30 03:09:00 【The coolness of Si Ku Quan Shu】
subject
Xiao Ming grabs a handful of candy at random from the candy box
Every time Xiao Ming takes out half of the candy and gives it to the students
When candy is not evenly distributed
Xiao Ming can take the candy box ( Suppose there are enough candy in the box ) Take out or put back a candy
How many times does Xiao Ming need at least ( Take it out, put it back and distribute it evenly ) Can divide the candy in your hand to only one
Input description :
Number of candy grabs ( Less than 1000000):15
Output description :
The number of times to get at least one candy :5
Example 1:
Input
15
Output
5
remarks
explain :(1) 15+1=16;
(2) 16/2=8;
(3) 8/2=4;
(4) 4/2=2;
(5) 2/2=1;
Code
Be careful : Is to count the number of times to retrieve and put in !!
#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;
}
// If it can be 2 to be divisible by , Never take it out or put it back ;
// If it can't be 2 to be divisible by , Unify +1 and -1 Who has the least !; Local optima are used here !!
if(i % 2 != 0){
if((i + 1)/2 % 2 == 0) i+=1;
else --i;
++count;
}
}
cout << count << endl;
return 0;
}
边栏推荐
- 2022 tool fitter (Advanced) and tool fitter (Advanced) certificate examination
- Raki's notes on reading paper: named entity recognition as dependency parsing
- MySQL extracts strings from table fields
- 广播模块代码在autojs4.1.1版本运行正常,但在pro7.0版本上运行报错(未解决)
- 问题记录:fel_lib.c:26:10: fatal error: libusb.h: 没有那个文件或目录
- 通用分页(2)
- 2022 underground coal mine electrical test and underground coal mine electrical simulation test
- What is the metauniverse: where are we, where are we going
- Cmake tutorial series -05- options and variables
- golang bilibili直播彈幕姬
猜你喜欢

【直播笔记0629】 并发编程二:锁

Heavy attack -- ue5's open source digital twin solution

Compile a DLL without import table

怎么利用Redis实现点赞功能
![[live broadcast notes 0629] Concurrent Programming II: lock](/img/5c/42f5c9a9969b4d2bb950a7caac5555.png)
[live broadcast notes 0629] Concurrent Programming II: lock

Cross domain, CORS, jsonp

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

What are outer chain and inner chain?

OP-diode-限制摆幅

中断操作:AbortController学习笔记
随机推荐
Summary of PHP test sites encountered in CTF questions (I)
O & M (20) make and start USB flash disk and install win10
Raki's notes on reading paper: discontinuous named entity recognition as maximum clique discovery
High paid programmers & interview questions series 63: talk about the differences between sleep (), yield (), join (), and wait ()
What is the concept of string in PHP
简单自定义MVC优化
怎么利用Redis实现点赞功能
约瑟夫环 数学解法
自定义MVC的使用
*Write a program to initialize a string object with a vector < char> container*/
How does the trading platform for speculation in spot gold ensure capital security?
一篇文章带你入门vim
Distributed file storage system fastdfs hands on how to do it
SQLite use
原生JS怎么生成九宫格
O & M (21) make winpe startup USB flash disk
简单自定义mvc
如果辨别我现在交易的外盘股指期货交易平台是否正规安全?
How to realize remote collaborative office, keep this strategy!
Formal and actual parameters, value passing and address passing