wojiaocbj

Untitled

Jun 9th, 2022
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 KB | None | 0 0
  1. /*
  2.  Author: 曹北健
  3.  Result: AC Submission_id: 4495171
  4.  Created at: Fri Jun 03 2022 10:20:30 GMT+0800 (China Standard Time)
  5.  Problem_id: 5844   Time: 53    Memory: 1732
  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. #ifdef _DEBUG
  20.     freopen("../../../in.txt", "r", stdin);
  21.     //freopen("../../../out.txt", "w", stdout);
  22. #endif // _DEBUG
  23.     LL a1, b1, a2, b2, a, b, g, s;
  24.     int op;
  25.     char input[256] = { 0 };
  26.     while(fgets(input, 255, stdin)){
  27.         sscanf(input, "%lld/%lld %lld/%lld %d", &a1, &b1, &a2, &b2, &op);
  28.         switch(op){
  29.         case 1:
  30.             a = (a1 * b2 + b1 * a2);
  31.             b = b1 * b2;
  32.             break;
  33.         case 2:
  34.             a = (a1 * b2 - b1 * a2);
  35.             b = b1 * b2;
  36.             break;
  37.         case 3:
  38.             a = a1 * a2;
  39.             b = b1 * b2;
  40.             break;
  41.         case 4:
  42.             a = a1 * b2;
  43.             b = b1 * a2;
  44.             break;
  45.         default:
  46.             break;
  47.         }
  48.         if(a < 0 || b < 0){
  49.             s = 1;
  50.         }
  51.         else{
  52.             s = 0;
  53.         }
  54.         a = llabs(a);
  55.         b = llabs(b);
  56.         g = gcd(a, b);
  57.         if(g == b){
  58.             printf("%lld\n", a / b);
  59.         }
  60.         else{
  61.             if(s){
  62.                 putchar('-');
  63.             }
  64.             if(g != 1){
  65.                 printf("%lld/%lld\n", a / g, b / g);
  66.             }
  67.             else{
  68.                 printf("%lld/%lld\n", a, b);
  69.             }
  70.         }
  71.     }
  72. #ifdef _DEBUG
  73.     freopen("CON", "r", stdin);
  74.     //freopen("CON", "w", stdout);
  75.     system("pause");
  76. #endif // _DEBUG
  77.  
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment