dzungchaos

C++ "Chuyển hệ cơ số 2 (dùng stack)"

Jan 15th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4.  
  5. void binary(int m){
  6. if (m > 0){
  7. binary(m / 2);
  8. cout << m % 2;
  9. }
  10. }
  11.  
  12. void binary_stk(int m){
  13. stack<int> s;
  14. while (m > 0){
  15. s.push(m % 2);
  16. m = m / 2;
  17. }
  18. while(!s.empty()){
  19. cout << s.top();
  20. s.pop();
  21. }
  22. }
  23.  
  24. int main(){
  25. int m;
  26. cout << "m = "; cin >> m;
  27. binary(m); cout << endl;
  28. binary_stk(m);
  29. }
Advertisement
Add Comment
Please, Sign In to add comment