fork(1) download
  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4.  
  5. int gcd(int a, int b) {
  6. while (b != 0) {
  7. int t = a % b;
  8. a = b;
  9. b = t;
  10. }
  11. return a;
  12. }
  13.  
  14. int main() {
  15. int a, b;
  16. cin >> a >> b;
  17.  
  18. // skracamy
  19. int g = gcd(a, b);
  20. a /= g;
  21. b /= g;
  22.  
  23. if (a >= b) {
  24. cout << "Ulamek nie jest wlasciwy\n";
  25. return 0;
  26. }
  27.  
  28. cout << "0.";
  29.  
  30. map<int, int> seen;
  31. string result = "";
  32.  
  33. int r = a;
  34. int pos = 0;
  35.  
  36. // Limit -> aby IDEONE nie ucinal procesu
  37. const int MAX_DIGITS = 5000;
  38.  
  39. while (pos < MAX_DIGITS) {
  40. if (r == 0) {
  41. cout << result << "\n";
  42. return 0;
  43. }
  44.  
  45. if (seen.count(r)) {
  46. int start = seen[r];
  47. cout << result.substr(0, start)
  48. << "("
  49. << result.substr(start)
  50. << ")\n";
  51. return 0;
  52. }
  53.  
  54. seen[r] = pos;
  55. pos++;
  56.  
  57. r *= 2;
  58.  
  59. if (r >= b) {
  60. result += '1';
  61. r -= b;
  62. } else {
  63. result += '0';
  64. }
  65. }
  66.  
  67. cout << "Przerwano (za dlugi okres na IDEONE)\n";
  68. return 0;
  69. }
  70.  
  71.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Ulamek nie jest wlasciwy