wojiaocbj

Untitled

Jun 2nd, 2022
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. /*
  2.  Author: 曹北健
  3.  Result: AC Submission_id: 4495176
  4.  Created at: Fri Jun 03 2022 10:21:38 GMT+0800 (China Standard Time)
  5.  Problem_id: 5844   Time: 52    Memory: 1740
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11. #include <ctype.h>
  12. #include <string.h>
  13. #pragma warning(disable:4996)
  14. typedef long long LL;
  15. LL gcd(LL x, LL y){
  16.     return x % y ? gcd(y, x % y) : y;
  17. }
  18. int main(){
  19.     LL a1, b1, a2, b2, a, b, g, s;
  20.     int op;
  21.     char input[256] = { 0 };
  22.     while(fgets(input, 255, stdin)){
  23.         sscanf(input, "%lld/%lld %lld/%lld %d", &a1, &b1, &a2, &b2, &op);
  24.         switch(op){
  25.         case 1:
  26.             a = (a1 * b2 + b1 * a2);
  27.             b = b1 * b2;
  28.             break;
  29.         case 2:
  30.             a = (a1 * b2 - b1 * a2);
  31.             b = b1 * b2;
  32.             break;
  33.         case 3:
  34.             a = a1 * a2;
  35.             b = b1 * b2;
  36.             break;
  37.         case 4:
  38.             a = a1 * b2;
  39.             b = b1 * a2;
  40.             break;
  41.         default:
  42.             break;
  43.         }
  44.         if(a < 0 || b < 0){
  45.             s = 1;
  46.         }
  47.         else{
  48.             s = 0;
  49.         }
  50.         a = llabs(a);
  51.         b = llabs(b);
  52.         g = gcd(a, b);
  53.         if(g == b){
  54.             printf("%lld\n", a / b);
  55.         }
  56.         else{
  57.             if(s){
  58.                 putchar('-');
  59.             }
  60.             if(g != 1){
  61.                 printf("%lld/%lld\n", a / g, b / g);
  62.             }
  63.             else{
  64.                 printf("%lld/%lld\n", a, b);
  65.             }
  66.         }
  67.     }
  68.     retur
Advertisement
Add Comment
Please, Sign In to add comment