Advertisement
wojiaocbj

Problem.G

Jun 9th, 2022
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef long long LL;
  4. LL gcd(LL x, LL y){
  5.     return x % y ? gcd(y, x % y) : y;
  6. }
  7. int main(){
  8.     LL a1, b1, a2, b2, a, b, g, s;
  9.     int op;
  10.     char input[256] = { 0 };
  11.     while(fgets(input, 255, stdin)){
  12.         sscanf(input, "%lld/%lld %lld/%lld %d", &a1, &b1, &a2, &b2, &op);
  13.         switch(op){
  14.         case 1:
  15.             a = (a1 * b2 + b1 * a2);
  16.             b = b1 * b2;
  17.             break;
  18.         case 2:
  19.             a = (a1 * b2 - b1 * a2);
  20.             b = b1 * b2;
  21.             break;
  22.         case 3:
  23.             a = a1 * a2;
  24.             b = b1 * b2;
  25.             break;
  26.         case 4:
  27.             a = a1 * b2;
  28.             b = b1 * a2;
  29.             break;
  30.         default:
  31.             break;
  32.         }
  33.         if(a < 0 || b < 0){
  34.             s = 1;
  35.         }
  36.         else{
  37.             s = 0;
  38.         }
  39.         a = llabs(a);
  40.         b = llabs(b);
  41.         g = gcd(a, b);
  42.         if(g == b){
  43.             printf("%lld\n", a / b);
  44.         }
  45.         else{
  46.             if(s){
  47.                 putchar('-');
  48.             }
  49.             if(g != 1){
  50.                 printf("%lld/%lld\n", a / g, b / g);
  51.             }
  52.             else{
  53.                 printf("%lld/%lld\n", a, b);
  54.             }
  55.         }
  56.     }
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement