Source code for submission s1033

fl.c

  1. #include <math.h>
  2. #include <stdio.h>
  3.  
  4. int main()
  5. {
  6. int n, dummy;
  7.  
  8. while (scanf("%d/%d", &dummy, &n) == 2) {
  9. int x, y, cnt = 0;
  10. x = y = 2 * n;
  11.  
  12. while (y > n) {
  13. double res = ((double) x / (x + y)) * y;
  14. if (res == n) {
  15. //printf("1/%d + 1/%d = 1%d\n", x, y, n);
  16. cnt++;
  17. x++;
  18. } else if (res > n) {
  19. int newy = floor(((double) x / (x - n)) * n);
  20. if (newy < y) {
  21. y = newy;
  22. } else {
  23. break;
  24. }
  25. } else {
  26. int newx = ceil(((double) y / (y - n)) * n);
  27. if (newx > x) {
  28. x = newx;
  29. } else {
  30. break;
  31. }
  32. }
  33. }
  34. printf("%d\n", cnt);
  35. }
  36.  
  37. return 0;
  38. }
  39.