Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Gray code to binary code
- Minimum number of operations required to convert a binary number to 0 using the method:
- change ith binary digit only if (i+1)th binary digit is 1 and all digits following that is 0.
- **/
- #include <bits/stdc++.h>
- #define ll long long
- using namespace std;
- int main() {
- string S;
- cin>>S;
- ll fact = 1;
- int prev = S[0]-'0';
- ll ans = prev;
- for(ll i=1; i<S.size(); ++i){
- prev = prev^(S[i]-'0');
- ans = ans*2 + prev;
- }
- cout<<ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement