Poj Solution 3979

http://poj.org/problem?id=3979

#include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <math.h>
 
 int gdb(int a,int b)
 {
     int tmp,ret;
     if(a < b)
      {
    tmp = a;
    a = b;
    b =tmp;
      }
    while(b)
   {
      tmp = a;
      a = b;
      b = tmp%b;
    }
    ret = a;
    return ret;
}
int main(void)
{
    int a,b,c,d;
    char opt;
    int ans_a,ans_b;
    while(EOF != scanf("%d/%d%c%d/%d",&a,&b,&opt,&c,&d))
    {
        if('+' == opt)
          {
       ans_b = b*d;        ans_a = a*d+b*c;      
        a = gdb(ans_a,ans_b);
        ans_a /= a;
        ans_b /= a;
          }
        else
          {
        ans_b = b*d;
        ans_a = a*d-b*c;
        if(ans_a < 0)
          {
            a = gdb(-ans_a,ans_b);        
         }
        else
                  {
            a = gdb(ans_a,ans_b);
          }
        ans_a /= a;
        ans_b /= a;                
          }
       if(0 == ans_a)
        printf("0n");
       else
        {
           if(ans_a%ans_b == 0)
            printf("%dn",ans_a/ans_b);
            else
                    printf("%d/%dn",ans_a,ans_b);
         }
    
    }
    return 0;
}

											
This entry was posted in poj. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *