Result: Accepted
Time: 6ms
Memory: 1712kB
#include<iostream>
#include<cmath>
using namespace std;
int f(int a,int b){
if(a==0)
return b;
int t=a>b?a:b;
int s=a<b?a:b;
int m;
m=t%s;
while(m!=0){
t=s;
s=m;
m=t%s;
}
return s;
}
int main(){
int N;
int a1,b1,a0,b0;
int a=1;
cin>>N;
N=N-1;
char c;
cin>>a0>>c>>b0;
while(N--){
cin>>a1>>c>>b1;
a0=a0*b1+b0*a1;
b0=b0*b1;
int t=f(abs(a0),abs(b0));
a0/=t;
b0/=t;
}
if(a0==0){
cout<<0<<endl;
}
else{
if(a0%b0==0)
cout<<a0/b0<<endl;
else {
if(a0/b0!=0){
cout<<a0/b0<<" ";
a=0;
a0=a0-(a0/b0)*b0;
}
if(a==1&&a0*b0<0){
a0=abs(a0);
b0=abs(b0);
cout<<"-"<<a0<<"/"<<b0<<endl;
}
else{
a0=abs(a0);
b0=abs(b0);
cout<<a0<<"/"<<b0<<endl;
}
}
}
return 0;
}