fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main() {
  6. long long a, b; // licznik i mianownik
  7. cin >> a >> b;
  8.  
  9. // sprawdzenie czy mianownik jest potęgą 2
  10. if ((b & (b - 1)) != 0) {
  11. cerr << "Blad: mianownik nie jest potega 2." << endl;
  12. return 1;
  13. }
  14.  
  15. cout << "Rozwiniecie binarne: 0.";
  16.  
  17. // liczba bitów rozwinięcia = log2(b)
  18. int bits = 0;
  19. long long tmp = b;
  20. while (tmp > 1) {
  21. tmp >>= 1;
  22. bits++;
  23. }
  24.  
  25. // generowanie bitów
  26. long long numerator = a;
  27. for (int i = 0; i < bits; i++) {
  28. numerator *= 2;
  29. if (numerator >= b) {
  30. cout << "1";
  31. numerator -= b;
  32. } else {
  33. cout << "0";
  34. }
  35. }
  36.  
  37. cout << endl;
  38.  
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0.01s 5308KB
stdin
5 16
stdout
Rozwiniecie binarne: 0.0101