1

I do an exercise on Hackerrank:

Task
Given a base-10 integer, n, convert it to binary (base-2). Then find and print the base-10 integer denoting the maximum number of consecutive 1's in n's binary representation. When working with different bases, it is common to show the base as a subscript.

Example

The binary representation of 125 is 1111101. In base 2, there are 5 and 1 consecutive ones in two groups. Print the maximum, 5.

My solution is: I create a stack, push binary converted to it, and then I put it into new string. But when I run in dev-C++, the output I receive is just special characters. Can you explain to me and let me know how to fix?

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    stack<char>st;
    int temp;
    while(n!=0){
        temp = n%2;
        n=n/2;
        st.push(temp);
    }
        string s;

while(st.empty() == false){
    s = s + st.top();
    st.pop();
    }
cout << s;    // I try input n=5 but output is :) :)

// Count the maximum number of consecutive 1's
    int d=0;
    int max=0;
    for (int i=0; i<s.size(); i++){
        if (s[i]=='1') d++;
        if (s[i]=='0'){
            if (d>max) {
                max=d;
                d=0;
            }
            else d=0;
        }
    }
    cout << max;
    return 0;
}
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • 4
    `st.push(temp + '0');` ? – rafix07 Dec 18 '20 at 13:58
  • It would really help if you broke this up into functions, each of which did *one* thing at a time, like one that converts to the binary string, and another that given a binary string returns the maximum consecutive things. This way you can test each individually and verify they're correct before integrating them in your final app. – tadman Dec 18 '20 at 14:02
  • oh it works! Can you explain me why need to put '0'? – f_for_myself Dec 18 '20 at 14:04
  • Because 0 is different from '0'. Consult an ASCII table. – drescherjm Dec 18 '20 at 14:18
  • 3
    [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). [Why should I not #include ?](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.) – Gary Strivin' Dec 18 '20 at 14:21
  • In Lisp this would be a 1 liner - and even then more readable ;) – BitTickler Dec 18 '20 at 14:26
  • `(let ((x (parse-integer (read-line *standard-input* nil)))) (loop for s in (ppcre:split "0" (format nil "~B" x)) maximizing (length s)))` – BitTickler Dec 18 '20 at 14:35
  • You do not need to do any conversion dec to bin or vice versa. This is done by standard library. Just focus on counting constructive `1` in value. You can do it directly in `int` type. Just fix `consbtcnt` in [this code](https://godbolt.org/z/Mbczo4) (code is not correct on purpose). – Marek R Dec 18 '20 at 14:44
  • 1
    This doesn't address the question, but `while(st.empty() == false)` is usually written `while(!st.empty())`. – Pete Becker Dec 18 '20 at 15:55

0 Answers0