1

Im currently working on https://open.kattis.com/problems/rationalarithmetic for own practice. I get 4 digits and a operation. The input is: x1 y1 op x2 y2 and the fraction is x1/y1 and x2/y2. If I get the input: 1 3 + 1 2 then its 1/3 + 1/2 and the answer should be given the minimal fractial so its 5/6. I pass the testcases I get and I cant figure out what Im doing wrong. Summarize what I do:

  1. Read input and check the operation if its +,-,/ or *. I generate a prime array to find the biggest common divisor.
  2. Send the input to a function depending on which operation it is.
  3. I count the given input with simple math.
  4. Then i find the biggest common divisor and divide both numerator and denominator with this.
  5. After that i print out in the result.

Here is main function and how I handle if the operation is *. I handle the other operation the same but with other math.

     void mult(int *x1, int *y1, int *x2, int *y2){
      long long top = (*x1) * (*x2);
      long long bottom = (*y2) * (*y1);
      long long frac;
      if(bottom != 0||top != 0){
          frac = commonDiv(top,bottom);
     }else{
         frac = 1;
     }
     string sign = "";
     if(top * bottom < 0){
         sign = "-";
      }else{
         sign = "";
      }
     printf("%s%lld / %lld\n",sign.c_str(),abs(top/frac),abs(bottom/frac) );
    }


     int main()
     {

     int numOp;
     scanf("%d", &numOp);
     getPrime(1,sqrt(100000));
     while(numOp != 0){
      int x1,x2,y1,y2;
      char op[2];
      scanf("%d %d %s %d %d", &x1, &y1, op, &x2, &y2);
      if( op[0] == '+'){
        add(&x1, &y1, &x2,&y2);
      }
      else if(op[0] == '-'){
        sub(&x1,&y1,&x2,&y2);
      }
      else if(op[0] == '/'){
        divi(&x1,&y1,&x2,&y2);
     }
     else{
        mult(&x1,&y1,&x2,&y2);
    }
     numOp--;
    }   
    }

Here is my code wtih the given testcase and I get the correct result. I need some tips with either different testcases or any suggests. http://ideone.com/jBddSI

user3664730
  • 45
  • 1
  • 8

2 Answers2

1

I would adivse you the following: when dealing with rationals forget about finding biggest common divisor with primes list. That's what we were all taught at school but when programming this task can be much more easily (and efficiently) solved with Euclid's algorithm

marom
  • 5,064
  • 10
  • 14
  • Ye that true I changed my way to find out the biggest common divisor but I that was not the problem not sure what Im doing wrong. This is how I get the biggest common divisor now https://ideone.com/jBddSI – user3664730 Aug 05 '15 at 09:56
  • Thats the problem I dont know whats wrong with the code because I get the right output with given input so Im not sure what Im doing wrong – user3664730 Aug 05 '15 at 10:14
0

I don't understand why you are looking for primes only in range [1,10000] while the numbers you decompose can reach (10^9)^2 = 10^18 But as Marom said, you should use gdc algorithm instead.

Besides, I don't understand why are you passing pointers to int in your functions (add, divi...).

Also, in this code:

      long long top = (*x1) * (*x2);

I think the overflow happens if x1 and x2 are too big, because the cast happens after the multiplication of integers (and not long long int).

Brahim
  • 808
  • 1
  • 8
  • 17
  • I changed so Im passing pointers to long long and the input I read is long long. And i also changed so is gdc alogrithm. http://ideone.com/jBddSI – user3664730 Aug 05 '15 at 10:09
  • Well I meant don't pass pointers but copy the values :). Anyway, it still does not work? – Brahim Aug 05 '15 at 11:43