Created
May 30, 2020 01:56
-
-
Save Ch-sriram/930cd33f5ab4b6e83bbdb17a0a546ca0 to your computer and use it in GitHub Desktop.
Given an integer N, find its binary representation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
#define ull uint64_t | |
string bin_rep(ull n) { | |
if (n == 0) return ""; | |
string rem(1, (n&1) + '0'); // https://www.geeksforgeeks.org/how-to-convert-a-single-character-to-string-in-cpp/ | |
return bin_rep(n>>=1) + rem; | |
} | |
int main() { | |
int t; cin >> t; | |
while(t--) { | |
ull n; cin >> n; | |
string ans = (n == 0) ? "0" : bin_rep(n); | |
cout << ans << "\n"; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment