← Back to topics
Topic

z-easy?

K
Kameleon
Hi

I'm having trouble with the z-easy problem my code works perfectly when I compile it.

But when i send it i get WRONG RESULT for all of them.

can someone help with this :

Here is my C code:

#include <stdio.h>
long gcd(long a,long b);
int main(void)
{
long a,b,c,d;
scanf("%li/%li",&a,&b);
scanf("%li/%li",&c,&d);

long m=a*d+c*b;
long n=b*d;

long g_c_d=gcd(m,n);
printf("%li/%li\n",m/g_c_d,n/g_c_d);

return 0;
}
long gcd(long a,long b)
{
return (b==0?a:gcd(b,a%b));

}

Thanks a lot
A
Al3kSaNdaR
Try to scan in one scanf

scanf("%lld/%lld %lld/%lld",&a,&b,&c,&d)
i
iggy91
How could you read input with %li ? :-S
K
Kameleon
I tried scanning on one scanf but i get WRONG RESULT's for all of them again.

And i used %li to read long ints.

A
Al3kSaNdaR
Use long long and %lld for scan. ;)
t
tgudlek
long = int on 32bit computers
K
Kameleon
Al3kSaNdaR thanks a lot man it's working.

i
iggy91
@tgudlek
Yes, yes... I noticed that there is no difference in declaring variables as longs or ints. No matter how, it can store values up to 2^31... So, it's because of CPU architecture?
g
gigac
How can I read in pascal the rational numbers ?
How can I read this
scanf("%li/%li",&a,&b); 
?
A
Al3kSaNdaR
You must read a string and then parse it.
i
iggy91
You can simply read one int, then char, then again int:


long a,b;
char c;
scanf("%ld%c%ld",&a,&c,&b);

This will put char '/' in variable C, but it's not important. You'll have your rational number stored in A and B.
g
gigac
Why I get the Exit code is not zero, for all tests ?
t
tgudlek
Try not to use CRT
g
gigac
The problem is not in CRT.
I still get Exit code is not zero
A
Al3kSaNdaR
You must read input in following order. Read one string from entire line and then "chop" it to pieces. Use
 Pos , Copy , Delete , Val , ...  
functions. This is the only way to do this task in Pascal.
g
gigac
Thanks..I will try :)
A
Al3kSaNdaR
No problem, ask if you need help. :)
g
gigac
Why I get wrong result for all of the tests :(
A
Al3kSaNdaR
1. Don't use CLRSCR and CRT module!
2. Don't use ReadLn on bottom of your code!
3. You could WriteLn just with WriteLn because LongInt numbers are in 0:0 format.
g
gigac
It doesn't matter.
I try in other way without Crt and that stuff. I get Wrong Result...
This is my code

Var a,b,c,d : LongInt;
x,y,z : LongInt;
m,n : LongInt;
i : LongInt;
err : Integer;
S,H : String;

Function gcd(q,r : Integer): integer;
Begin
if (r=0) then gcd:=q else
gcd:=gcd(r,q mod r);
End;

Procedure kra(p,o : LongInt; var z1,w1 : LongInt);
Var R : Integer;
Begin
R:=gcd(p,o);
z1:=p div r;
w1:=o div r;
End;

Begin
ReadLn(S);

I:=Pos('/',S);
H:=Copy(S,1,I-1);
Delete(S,1,Length(H)+1);
Val(H,A,err);

I:=Pos(' ',S);
H:=Copy(S,1,I-1);
Delete(S,1,Length(H)+1);
Val(H,B,err);

I:=Pos('/',S);
H:=Copy(S,1,I-1);
Delete(S,1,Length(H)+1);
Val(H,C,err);

Val(S,D,err);

kra(a,b,x,y);
kra(c,d,m,n);
kra( ((x*n)+(y*m)),y*n,a,b);

WriteLn(a,'/',b);
End.
N
Nocturne
@gigac
Since the input numbers may vary up to 10^9, their multiple can exceed 2^32. Try using int64 instead of longint.
g
gigac
Same...
Really, what's the problem ! :@
Just copy this code, try it and you'll see.
I get a correct answer.. admins please help :S
N
Nocturne
@gigac
Replace EVERY declared INTEGER by INT64, including the parameter types in your functions and procedures. Remember not to use int64 for the ERROR code in the VAL procedure as it cannot be assigned an int64 value.

Var a,b,c,d : int64;
x,y,z : int64;
m,n : int64;
i : Int64;
err : integer;
S,H : String;

Function gcd(q,r : Int64): int64;
Begin
if (r=0) then gcd:=q else
gcd:=gcd(r,q mod r);
End;

Procedure kra(p,o : Int64; var z1,w1 : Int64);
Var R : Int64;
Begin
R:=gcd(p,o);
z1:=p div r;
w1:=o div r;
End;

Begin
ReadLn(S);


I:=Pos('/',S);
H:=Copy(S,1,I-1);
Delete(S,1,Length(H)+1);
Val(H,A,err);


I:=Pos(' ',S);
H:=Copy(S,1,I-1);
Delete(S,1,Length(H)+1);
Val(H,B,err);

I:=Pos('/',S);
H:=Copy(S,1,I-1);
Delete(S,1,Length(H)+1);
Val(H,C,err);

Val(S,D,err);


kra(a,b,x,y);
kra(c,d,m,n);
kra( ((x*n)+(y*m)),y*n,a,b);

WriteLn(a,'/',b);

End.
g
gigac
Yeaaaaaaaaaaaaaaaaa.....
Thanks a lot Nocturne :)