-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_to_binary.cpp
More file actions
41 lines (40 loc) · 881 Bytes
/
string_to_binary.cpp
File metadata and controls
41 lines (40 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<bits/stdc++.h>
using namespace std;
string div(string number)
{
int divisor=2;
string ans;
int idx = 0;
int temp = number[idx] - '0';
while (temp < divisor)
temp = temp * 10 + (number[++idx] - '0');
while (number.size() > idx)
{
ans += (temp / divisor) + '0';
temp = (temp % divisor) * 10 + number[++idx] - '0';
}
// cout<<ans<<endl;
if (ans.length() == 0)
return "0";
return ans;
}
string s1="";
void fn(string str){
int n=str.length();
int x=str[n-1]-'0';
if(x&1){ s1+='1'; ;str[n-1]= str[n-1]-1;}
else s1+='0';
if((x==0 || x==1) && n==1){return;}
string rec=div(str);
fn(rec);
}
int main()
{
string s;
cin>>s;
cout<<s.length()<<endl;
fn(s);
reverse(s1.begin(),s1.end());
cout<<s1.length()<<endl; // binary string
return 0;
}