fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4.  
  5. int a, b, c, d, e, f, g, h, i, j;
  6. scanf ("%d%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f, &g);
  7.  
  8. //代分數換假分數
  9. int x1, x2;
  10. if ( a > 0 )
  11. x1 = a * c + b;
  12. else
  13. x1 = a * c - b;
  14. if ( e > 0 )
  15. x2 = e * g + f;
  16. else
  17. x2 = e * g - f;
  18.  
  19. //printf ("%d / %d\n", x1, c);
  20. //printf ("%d / %d\n", x2, g);
  21. //printf ("%d\n", d);
  22.  
  23. //假分數加減乘除
  24. switch (d) {
  25. case 0:
  26. i = (x1 * g) + (x2 * c);
  27. j = c * g;
  28. break;
  29. case 1:
  30. i = (x1 * g) - (x2 * c);
  31. j = c * g;
  32. break;
  33. case 2:
  34. i = x1 * x2;
  35. j = c * g;
  36. break;
  37. case 3:
  38. i = x1 * g;
  39. j = c * x2;
  40. break;
  41. default:
  42. printf ("error");
  43. }
  44.  
  45. //printf ("%d / %d\n", i, j);
  46.  
  47. if ( i < 0 ) {
  48. i = -i;
  49. h = - ( i / j );
  50. } else if ( i > 0) {
  51. h = i / j;
  52. }
  53. i %= j;
  54.  
  55. //printf ("%d %d %d\n", h, i, j);
  56.  
  57. if ( i == 0 ) {
  58. j = 1;
  59. } else {
  60. int temp1 = i;
  61. int temp2 = j;
  62. int temp;
  63. while ( temp2 % temp1 != 0 ) {
  64. temp = temp2 % temp1;
  65. temp2 = temp1;
  66. temp1 = temp;
  67. }
  68. i /= temp1;
  69. j /= temp1;
  70. }
  71.  
  72. printf ("%d %d %d", h, i, j);
  73.  
  74. return 0;
  75. }
  76.  
Success #stdin #stdout 0.01s 5292KB
stdin
2
0
1
2
-1
1
3
stdout
-2 2 3