fork(1) download
  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4.  
  5. int main() {
  6. int a = 2, b = 5;
  7.  
  8. cout << "Rozwiniecie binarne 2/5: 0.";
  9.  
  10. map<int,int> seen;
  11. string result = "";
  12. int pos = 0;
  13. int r = a;
  14.  
  15. while (true) {
  16. if (r == 0) {
  17. cout << result << endl;
  18. break;
  19. }
  20. if (seen.count(r)) {
  21. int start = seen[r];
  22. cout << result.substr(0,start) << "(" << result.substr(start) << ")" << endl;
  23. break;
  24. }
  25. seen[r] = pos++;
  26. r *= 2;
  27. if (r >= b) {
  28. result += '1';
  29. r -= b;
  30. } else {
  31. result += '0';
  32. }
  33. }
  34.  
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Rozwiniecie binarne 2/5: 0.(0110)