简体   繁体   English

计算二进制的连续1

[英]count consecutive 1's in binary

I am writing code in Hackerrank. 我正在用Hackerrank编写代码。 And recently the problem said, convert decimal to base 2 and then count the max consecutive 1's in the binary number. 最近出现的问题是,将十进制转换为基数2,然后计算二进制数中的最大连续1。 And first I come with following solution. 首先,我提出以下解决方案。 It works fine. 工作正常。 But I do not understand the counting part of it, even though I wrote it. 但是即使我写了它,我也不理解它的计数部分。

The code is 该代码是

int main(){
    int n,ind=0, count=0, mmax=0;
    char bin[100];
    cin >> n;
    while(n){
        if(n%2==0) {
            bin[ind]='0';
            n = n / 2;
            ind = ind + 1; 
            }
        else if(n%2==1) {
            bin[ind]='1';
            n = n / 2;
            ind = ind + 1;    
        }
    }
    for(int i=0; i<=(ind-1); i++){
        if(bin[i] == '1' && bin[i+1] == '1'){
            count++; 
            if(mmax < count)
                mmax = count;
        }
        else
            count=0;
    }
    cout << mmax + 1 << endl;
    return 0;
    }

In the above code, I guess that variable mmax will give me the max consecutive number of 1's but it gives me value that has (max consecutive - 1), So I just wrote like that and submitted the code. 在上面的代码中,我想变量mmax会给我最大的连续数1,但它给我的值(最大连续数-1),所以我只是这样写并提交了代码。 But I am curious about. 但是我很好奇。 why it is working that way. 为什么这样工作。 I am little bit of confused the way that code works like this. 我有点困惑代码的工作方式。

Thanks 谢谢

Lets say you have this binary sequence: 假设您有以下二进制序列:

11110

Your code will compare starting from the first and second: 您的代码将从第一个和第二个开始进行比较:

|11|110 1 && 1 -> max = 1
1|11|10 1 && 1 -> max = 2
11|11|0 1 && 1 -> max = 3
111|10| 1 && 0 -> max = 3

you can see, that although there are 4 1's you only do 3 comparisons, so your max will always be -1 of the actual max. 您会看到,尽管有4个1,但是您只进行了3个比较,因此您的最大值始终是实际最大值的-1。 You can fix this by adding mmax += 1 after your for loop. 您可以通过在for循环后添加mmax += 1来解决此问题。

Just a little bit of trace using small example will show why. 仅通过一些小例子来说明原因。

First, lets say there is only 1 '1' in your array. 首先,假设阵列中只有1个“ 1”。

Since you require both the current position and your next position to be '1', you will always get 0 for this case. 由于您要求当前位置和下一个位置都为“ 1”,因此在这种情况下,您总是会得到0。

Let's say I have "11111". 假设我有“ 11111”。 At the first '1', since next position is also '1', you increment count once. 在第一个“ 1”处,由于下一个位置也是“ 1”,因此您要递增一次计数。 This repeats until 4th '1' and you increment your count 4 times in total so far. 一直重复到第4个“ 1”,到目前为止,您的计数总共增加了4次。 When you reach 5th '1', your next position is not '1', thus your count stops at 4. 当您达到第5个“ 1”时,您的下一个位置不是“ 1”,因此您的计数停止在4。

In general, your method is like counting gaps between fingers, given 5 fingers, you get 4 gaps. 通常,您的方法就像计算手指之间的间隙,给定5个手指,您将得到4个间隙。

Side note: your code will fail for the case when there is no '1' in your array. 旁注:如果您的阵列中没有'1',则您的代码将失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM