Result: Accepted
Time: 6ms
Memory: 1716kB
#include<iostream>
#include<algorithm>
#include<set>
#include<string>
#include<cstring>
#include<cstdio>
#include<cmath>
const int maxn=1200;
using namespace std;
int main()
{
int a[maxn],b[maxn];
int n,m,p;
char ch;
cin>>n;
for(int i=0;i<n;++i)
{
cin>>a[i]>>ch>>b[i];
}
m=b[0];
for(int i=1;i<n;++i)
{
p=(b[i]*m)/__gcd(b[i],m);
m=p;
//printf("%d========\n",p);
}
int s=0;
for(int i=0;i<n;++i)
{
s+=p/b[i]*a[i];
//printf("%d---------\n",s);
}
int pp;
if(abs(s)>p&&abs(s)%p!=0)
{
pp=__gcd((abs(s)%p),p);
printf("%d %d/%d",s/p,(abs(s)%p)/pp,p/pp);
}
else if(abs(s)<p)
{
if(s==0) printf("0");
else
{
pp=__gcd(abs(s),p);
printf("%d/%d",s/pp,p/pp);
}
}
else
{
printf("%d",s/p);
}
}