突然想起今天拼多多笔试|刷题打卡

309 阅读1分钟

掘金团队号上线,助你 Offer 临门! 点击 查看详情

一、题目描述:

image.png

二、思路分析:

看起来有点复杂,我们注意到如果找最中间的一个的话会将数字分为差距最小的两组,奇数时差距为0。通过找规律发现这就是2**n的题

三、AC 代码:

#include<stdio.h>
#include<iostream>

using namespace std;

int main(){
    int c = 0;
    cin >> c;
    while(cin >> c){
        int ret = 1;
        while(c >= 2){
            c = c / 2;
            ret ++;
        }
        cout << ret << endl;
    }
    return 0;
}

四、总结:

这题作为第一题难度有点高,需要十分钟左右