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:
- Read input and check the operation if its +,-,/ or *. I generate a prime array to find the biggest common divisor.
- Send the input to a function depending on which operation it is.
- I count the given input with simple math.
- Then i find the biggest common divisor and divide both numerator and denominator with this.
- 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