fork(2) download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int n; cin >> n;
  8.  
  9. while (n--)
  10. {
  11. double x, y; cin >> x >> y;
  12. double l = x - y; double al = abs(l);
  13. double r = x + y; double ar = abs(r);
  14.  
  15. if (l * r < 0) { cout << 0 << endl; continue; }
  16. if (1.0 <= ar) { cout << (int)l << endl; continue; }
  17. if (l < 0) swap(al, ar);
  18.  
  19. bool hit = false;
  20. int nn, dd;
  21. for (int i = 1.0 / ar; !hit; ++i)
  22. {
  23. for (dd = 1; dd < i; ++dd)
  24. {
  25. nn = i - dd;
  26. double num = (double)nn / dd;
  27. if (al <= num && num <= ar) { hit = true; break; }
  28. }
  29. }
  30. cout << ((l < 0) ? "-" : "") << nn << "/" << dd << endl;
  31. }
  32.  
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 2.41s 3276KB
stdin
5
0.3 0.1
0.5 0.2
-0.3 0.1
0.07 0.02
0.0000000123 0.0000000003
stdout
1/3
1/2
-1/3
1/12
1/79365080